C#创建通用类型验证器服务

时间:2018-11-26 20:54:55

标签: c# validation service interface

在像老板和员工这样的企业中,我有一个人的结构。

我只是在每种类型上添加了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函数对于每种类型都是相同的。

1 个答案:

答案 0 :(得分:0)

您应该将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 * *
        }
    }