C#创建一个通用类型验证程序服务



我有一个企业中的人员结构,如Boss和Employee。

我只是在每种类型上添加了Validator,以便在继续创建之前应用规则。

public class BossService : IStructMapper<People>
{
IValidatorService<Boss> _validatorService;
public BossService(IValidatorService<Boss> validatorService)
{
_validatorService = validatorService;
}
public List<Boss> Convert(string json)
{
var bossSource = JsonConvert.DeserializeObject<List<boss>>(json);
bossSource.ForEach(x => x.ApplyRules());
_validatorService.ValidateContent(new BossValidator(), bossSource);
}
}

public class BossValidatorService : IValidatorService<Boss>
{
public void ValidateContent(AbstractValidator<Boss> validator, List<Boss> bossContent)
{
foreach (var item in bossContent)
{
var result = validator.Validate(item);
if (result.IsValid) continue;
}
}
}
public interface IValidatorService<T>
{
void ValidateContent(AbstractValidator<T> validator, List<T> content);
}

public class BossValidator : AbstractValidator<Boss>
{
public BossValidator()
{
***rules for boss**
}
}

所以在我的程序中,我调用它,它完成了它的工作:

var bosses = new BossService(new BossValidatorService()).Convert(datafile)

对于员工,我创建了不同的EmployeeService类和EmployeeValidator:

public class EmployeeService : IStructMapper<People>
{
IValidatorService<Employee> _validatorService;
public EmployeeService(IValidatorService<Employee> validatorService)
{
_validatorService = validatorService;
}
public List<Employee> Convert(string json)
{
var employeeSource = JsonConvert.DeserializeObject<List<Employee>>(json);
employeeSource .ForEach(x => x.ApplyRules());
_validatorService.ValidateContent(new BossValidator(), bossSource);
}
}
public class EmployeeValidator : AbstractValidator<Employee>
{
public EmployeeValidator()
{
***rules for employee**
}
}

我如何简化我的代码,使其不必创建BossValidatorService、EmployeeValidatorService、ConsultantValidatorService等,因为ValidateContent函数对每种类型都是相同的。

您应该将Type传递给class,并根据需要使用泛型。我没有去运行代码,但你应该清楚如何使用:

var bosses = new Service<Boss>(new ValidatorService<Boss, BossValidator>()).Convert(datafile)
public class Service<T, TValidator> : IStructMapper<People> where TValidator : new()
{
IValidatorService<T> _validatorService;
public Service(IValidatorService<T> validatorService)
{
_validatorService = validatorService;
}
public List<T> Convert(string json)
{
var Source = JsonConvert.DeserializeObject<List<T>>(json);
Source.ForEach(x => x.ApplyRules());
_validatorService.ValidateContent(new TValidator<T>(), Source);
}
}

public class ValidatorService<T> : IValidatorService<T> 
{
public void ValidateContent(AbstractValidator<T> validator, List<T> Content)
{
foreach (var item in Content)
{
var result = validator.Validate(item);
if (result.IsValid) continue;
}
}
}
public interface IValidatorService<T>
{
void ValidateContent(AbstractValidator<T> validator, List<T> content);
}

public class BossValidator : AbstractValidator<Boss>
{
public BossValidator()
{
***rules for boss * *
}
}

public class EmployeeValidator : AbstractValidator<Employee>
{
public EmployeeValidator()
{
***rules for employee * *
}
}

相关内容

  • 没有找到相关文章

最新更新