/// <summary> ///异步验证 /// </summary> public static async Task DoValidationAsync(IModuleRespository moduleRespository, Module module, string validatorType) { var moduleValidator = new ModuleValidator(moduleRespository); var validatorReresult = await moduleValidator.DoValidateAsync(module, validatorType); if (!validatorReresult.IsValid) { throw new DomainException(validatorReresult); } }
public ModuleValidator(IModuleRespository moduleRespository) { RuleSet(ValidatorTypeConstants.Create, () => { RuleFor(e => e.Name).Must((e, val) => !moduleRespository.Any(ea => ea.Name == val)) .WithMessage((e, val) => string.Format(ErrorMessage.IsNameRepeat, val)); BaseValidator(); }); RuleSet(ValidatorTypeConstants.Modify, () => { RuleFor(e => e.Name).Must((e, val) => !moduleRespository.Any(ea => ea.Name == val && ea.Id != e.Id)) .WithMessage((e, val) => string.Format(ErrorMessage.IsNameRepeat, val)); BaseValidator(); }); }
/// <summary> ///异步验证 /// </summary> public static async Task DoValidationAsync(IModuleRespository moduleRespository, IEnumerable <Module> modules, string validatorType) { var moduleValidator = new ModuleValidator(moduleRespository); var domainException = new DomainException(); foreach (var module in modules) { var validatorReresult = await moduleValidator.DoValidateAsync(module, validatorType); if (!validatorReresult.IsValid) { domainException.AddErrors(validatorReresult); } } if (domainException.ValidationErrors.ErrorItems.Any()) { throw domainException; } }
public ModuleAppService(IModuleRespository moduleRespository, IMapper mapper) { _moduleRespository = moduleRespository; _mapper = mapper; }