我正在尝试验证派生类,并且我还想验证基类。这是代码,并且在EmployeeValidator构造函数上出现以下错误:
没有给出与“ PersonValidator.PersonValidator”的所需形式参数“ personRepository”相对应的参数
public class Person
{
public string FullName { get; set; }
public int IDCard { get; set; }
}
public class Employee : Person
{
public string Position { get; set; }
public int EmployeeRecord { get; set; }
}
public class PersonValidator<T> : AbstractValidator<T> where T : Person
{
public PersonValidator(IPersonRepository personRepository)
{
RuleFor(b => b.FullName).NotNull();
RuleFor(x => x)
.Must(x => !personRepository.ExistsIDCard(x.IDCard))
.WithErrorCode("IDCardAlreadyExist");
}
}
public class EmployeeValidator : PersonValidator<Employee>
{
public EmployeeValidator(IEmployeeRepository employeeRepository)
{
RuleFor(d => d.Position).NotNull();
RuleFor(x => x)
.Must(x => !employeeRepository.ExistsEmployeeRecord(x.EmployeeRecord))
.WithErrorCode("EmployeeRecordAlreadyExist");
}
}
使用FluentValidation框架验证EmployeeValidator而不复制基类(人员)字段规则的合适方法是什么?
答案 0 :(得分:0)
您可以调用base
构造函数并将IPersonRepository
传递给它
public class EmployeeValidator : PersonValidator<Employee>
{
public EmployeeValidator(IEmployeeRepository employeeRepository, IPersonRepository personRepository)
: base(personRepository)
{
RuleFor(d => d.Position).NotNull();
RuleFor(x => x)
.Must(x => !employeeRepository.ExistsEmployeeRecord(x.EmployeeRecord))
.WithErrorCode("EmployeeRecordAlreadyExist");
}
}