/// <summary> ///异步验证 /// </summary> public static async Task DoValidationAsync(IOrganizationRespository organizationRespository, Organization organization, string validatorType) { var organizationValidator = new OrganizationValidator(organizationRespository); var validatorReresult = await organizationValidator.DoValidateAsync(organization, validatorType); if (!validatorReresult.IsValid) { throw new DomainException(validatorReresult); } }
public OrganizationValidator(IOrganizationRespository organizationRespository) { RuleSet(ValidatorTypeConstants.Create, () => { BaseValidator(); }); RuleSet(ValidatorTypeConstants.Modify, () => { BaseValidator(); }); }
public OrganizationValidator(IOrganizationRespository organizationRespository) { RuleSet(ValidatorTypeConstants.Create, () => { RuleFor(e => e.Name).Must((e, val) => !organizationRespository.Any(ea => ea.Name == val)) .WithMessage((e, val) => string.Format(ErrorMessage.IsNameRepeat, val)); BaseValidator(); }); RuleSet(ValidatorTypeConstants.Modify, () => { RuleFor(e => e.Name).Must((e, val) => !organizationRespository.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(IOrganizationRespository organizationRespository, IEnumerable <Organization> organizations, string validatorType) { var organizationValidator = new OrganizationValidator(organizationRespository); var domainException = new DomainException(); foreach (var organization in organizations) { var validatorReresult = await organizationValidator.DoValidateAsync(organization, validatorType); if (!validatorReresult.IsValid) { domainException.AddErrors(validatorReresult); } } if (domainException.ValidationErrors.ErrorItems.Any()) { throw domainException; } }
public OrganizationAppService(IOrganizationRespository organizationRespository, IMapper mapper) { _organizationRespository = organizationRespository; _mapper = mapper; }