我有一个抽象(没关系)的类:
public abstract class CarrierAbstractFormAPI
{
public string Name { get; set; }
public string Fein { get; set; }
public string McNumber { get; set; }
public string DotNumber { get; set; }
public AddressCreateAPI Address { get; set; }
}
和AddressCreateAPI类:
public class AddressCreateAPI
{
public string Street { get; set; }
public string City { get; set; }
public string ZipPostalCode { get; set; }
public int StateProvinceId { get; set; }
public string ContactName { get; set; }
public string ContactPhone { get; set; }
public string ContactFaxNumber { get; set; }
public string ContactEmail { get; set; }
}
我的验证者:
public abstract class CarrierAbstractFluentValidation<T> : AbstractValidator<T> where T : CarrierAbstractFormAPI
{
public CarrierAbstractFluentValidation()
{
RuleFor(d => d.Name)
.NotEmpty().WithMessage("Name is required");
RuleFor(d => d.Fein)
.NotEmpty().WithMessage("Fein is required");
RuleFor(d => d.McNumber)
.NotEmpty().WithMessage("McNumber is required");
RuleFor(d => d.DotNumber)
.NotEmpty().WithMessage("DotNumber is required");
RuleFor(d => d.Address.Street)
.NotEmpty().WithMessage("Address Street is required");
RuleFor(d => d.Address.City)
.NotEmpty().WithMessage("Address City is required");
RuleFor(d => d.Address.StateProvinceId)
.InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");
}
}
它工作正常。
但是我还有一些其他的课程,
public AddressCreateAPI Address { get; set; }
属性。
我要移动一部分:
RuleFor(d => d.Address.Street)
.NotEmpty().WithMessage("Address Street is required");
RuleFor(d => d.Address.City)
.NotEmpty().WithMessage("Address City is required");
RuleFor(d => d.Address.StateProvinceId)
.InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");
应用于普通班级,并将其应用于我所有的流利的验证器中,该验证器具有地址属性。怎么做?
最佳答案
想一想,您需要做的就是重用验证器类。
class AddressCreateAPIValidator : AbstractValidator<AddressCreateAPI>
{
public AddressCreateAPIValidator()
{
RuleFor(d => d.Street)
.NotEmpty().WithMessage("Address Street is required");
RuleFor(d => d.City)
.NotEmpty().WithMessage("Address City is required");
RuleFor(d => d.StateProvinceId)
.InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");
}
}
class SomeClass
{
public AddressCreateAPI Prop { get; set; }
}
class SomeClassValidator : AbstractValidator<SomeClass>
{
public SomeClassValidator()
{
RuleFor(d => d.Prop).SetValidator(new AddressCreateAPIValidator());
}
}
请注意,
AddressCreateAPIValidator
如何提取用于验证AddressCreateAPI
类的通用逻辑,然后通过使用对SetValidator
的调用将其重新用于属性。如果要创建通用验证器,可以将其与其他答案中的基于反射的方法混合并匹配。