public class Address
{
public string Address { get; set; }
public string City { get; set; }
}
public class AddressValidator : AbstractValidator<Address>
{
public AddressValidator()
{
RuleFor(x => x.Address).NotEmpty().WithMessage("The Address cannot be empty.");
RuleFor(x => x.City).NotEmpty().WithMessage("The City cannot be empty.");
}
}
public class PersonBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address PermanentAddress { get; set; }
}
public class PersonGroupA : PersonBase
{
public int TotalSaleCount { get; set; }
}
public class PersonGroupB : PersonBase
{
public int TodaySaleCount { get; set; }
}
public class PersonBaseValidator : AbstractValidator<PersonBase>
{
public PersonBaseValidator()
{
RuleFor(x => x.FirstName).NotEmpty().WithMessage("The First Name cannot be empty.");
RuleFor(x => x.LastName).NotEmpty().WithMessage("The Last Name cannot be empty.");
RuleFor(x => x.PermanentAddress).SetValidator(new AddressValidator());
}
}
public class PersonGroupAValidator : AbstractValidator<PersonGroupA>
{
public PersonGroupAValidator()
{
RuleFor(x => x.TotalSaleCount).NotEmpty().WithMessage("The Total Sale Count cannot be empty.");
}
}
public class PersonGroupBValidator : AbstractValidator<PersonGroupB>
{
public PersonGroupBValidator()
{
RuleFor(x => x.TodaySaleCount).NotEmpty().WithMessage("The Today Sale Count cannot be empty.");
}
}
子バリデーターでベースバリデーターを使用する方法
これは、以下のように追加すると機能します
public class PersonGroupAValidator : AbstractValidator<PersonGroupA> {
public PersonGroupAValidator() {
Include(new PersonBaseValidator());
RuleFor(x => x.TotalSaleCount).NotEmpty().WithMessage("The Total Sale Count cannot be empty.");
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加