/// <summary> /// 验证实例 /// </summary> /// <typeparam name="T">实例类型</typeparam> /// <param name="instance">实例对象</param> public static void Validate <T>(T instance) { //获取验证者实例集 IEnumerable <IValidator <T> > validators = ResolveMediator.ResolveAll <IValidator <T> >(); //构造异常消息 StringBuilder builder = new StringBuilder(); foreach (IValidator <T> validator in validators) { //得到验证结果 ValidationResult validationResult = validator.Validate(instance); //如果未验证通过 if (!validationResult.IsValid) { foreach (string message in validationResult.Errors.Select(x => x.ErrorMessage)) { builder.Append(message); builder.Append("/"); } } } //说明验证未通过 if (builder.Length > 0) { string errorMessaage = builder.ToString().Substring(0, builder.Length - 1); throw new ValidateFailedException(errorMessaage); } }
/// <summary> /// 解析多次注册的服务 /// </summary> /// <param name="serviceType">所请求的服务的类型</param> /// <returns>请求的服务</returns> public virtual IEnumerable <object> GetServices(Type serviceType) { if (WebApiDependencyResolver.OnGetInstance != null) { WebApiDependencyResolver.OnGetInstance.Invoke(); } return(ResolveMediator.ResolveAll(serviceType)); }
/// <summary> /// 清理数据库 /// </summary> public static void CleanDb() { IEnumerable <IDbCleaner> dbCleaners = ResolveMediator.ResolveAll <IDbCleaner>(); foreach (IDbCleaner dbCleaner in dbCleaners) { dbCleaner.Clean(); } }
/// <summary> /// 获取定时任务执行者实例列表 /// </summary> /// <param name="crontabType">定时任务类型</param> /// <returns>定时任务执行者列表</returns> public static IEnumerable <ICrontabExecutor> GetCrontabExecutorsFor(Type crontabType) { #region # 验证类型 if (!typeof(ICrontab).IsAssignableFrom(crontabType)) { throw new InvalidOperationException($"类型\"{crontabType.FullName}\"不实现定时任务基接口!"); } #endregion Type schedulerType = typeof(ICrontabExecutor <>).MakeGenericType(crontabType); IEnumerable <object> schedulerInstances = ResolveMediator.ResolveAll(schedulerType); IEnumerable <ICrontabExecutor> schedulers = schedulerInstances.Select(handler => (ICrontabExecutor)handler); return(schedulers); }
/// <summary> /// 获取领域事件处理者实例列表 /// </summary> /// <param name="eventType">领域事件类型</param> /// <returns>领域事件处理者列表</returns> public static IEnumerable <IEventHandler> GetEventHandlersFor(Type eventType) { #region # 验证类型 if (!typeof(IEvent).IsAssignableFrom(eventType)) { throw new InvalidOperationException($"类型\"{eventType.FullName}\"不实现领域事件基接口!"); } #endregion Type handlerType = typeof(IEventHandler <>).MakeGenericType(eventType); IEnumerable <object> handlerInstances = ResolveMediator.ResolveAll(handlerType); IEnumerable <IEventHandler> handlers = handlerInstances.Select(handler => (IEventHandler)handler); return(handlers); }
/// <summary> /// 解析服务实例列表 /// </summary> /// <param name="service">服务类型</param> /// <returns>服务实例列表</returns> protected override IEnumerable <object> GetAllInstances(Type service) { IEnumerable <object> instances = ResolveMediator.ResolveAll(service); return(instances); }
/// <summary> /// 获取定时任务执行者实例列表 /// </summary> /// <typeparam name="T">定时任务类型</typeparam> /// <param name="crontab">定时任务</param> /// <returns>定时任务执行者列表</returns> public static IEnumerable <ICrontabExecutor <T> > GetCrontabExecutorsFor <T>(T crontab) where T : class, ICrontab { IEnumerable <ICrontabExecutor <T> > schedulers = ResolveMediator.ResolveAll <ICrontabExecutor <T> >(); return(schedulers); }
/// <summary> /// 解析多次注册的服务 /// </summary> /// <param name="serviceType">所请求的服务的类型</param> /// <returns>请求的服务</returns> public IEnumerable <object> GetServices(Type serviceType) { return(ResolveMediator.ResolveAll(serviceType)); }
/// <summary> /// 获取领域事件处理者实例列表 /// </summary> /// <typeparam name="T">领域事件类型</typeparam> /// <param name="eventSource">领域事件源</param> /// <returns>领域事件处理者列表</returns> public static IEnumerable <IEventHandler <T> > GetEventHandlersFor <T>(T eventSource) where T : class, IEvent { IEnumerable <IEventHandler <T> > handlers = ResolveMediator.ResolveAll <IEventHandler <T> >(); return(handlers); }