Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#应用程序中使用FluentValidation_C#_Entity Framework_Asp.net Web Api_Business Logic_Fluentvalidation - Fatal编程技术网

如何在c#应用程序中使用FluentValidation

如何在c#应用程序中使用FluentValidation,c#,entity-framework,asp.net-web-api,business-logic,fluentvalidation,C#,Entity Framework,Asp.net Web Api,Business Logic,Fluentvalidation,我正在构建一个具有以下层的应用程序 数据实体框架上下文 实体-实体框架POCO对象 服务-由WebApi调用以加载/保存实体 WebApi- 现在我相信我应该把我的业务逻辑放到服务层,因为我有一个实体服务,例如,我有一个家庭对象和一个家庭服务 要使用FluentValidation创建验证对象,似乎必须从AbstractValidator继承,因为我的服务已经从一个对象继承,这是不可能的(或者是不可能的) 我想我唯一的选择是在服务层中创建一个FamilyValidator并从服务中调用这个验证器

我正在构建一个具有以下层的应用程序

数据实体框架上下文 实体-实体框架POCO对象 服务-由WebApi调用以加载/保存实体 WebApi-

现在我相信我应该把我的业务逻辑放到服务层,因为我有一个实体服务,例如,我有一个家庭对象和一个家庭服务

要使用FluentValidation创建验证对象,似乎必须从AbstractValidator继承,因为我的服务已经从一个对象继承,这是不可能的(或者是不可能的)

我想我唯一的选择是在服务层中创建一个FamilyValidator并从服务中调用这个验证器


fluentValidation是我最好的选择,还是我把这里的事情弄糊涂了?

如果您有一个名为Customer的实体,您可以这样为它编写验证器:

public class CustomerValidator: AbstractValidator<Customer> {
  public CustomerValidator() {
    RuleFor(customer => customer.Surname).NotEmpty();
    RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
    RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
    RuleFor(customer => customer.Address).Length(20, 250);
    RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
  }

  private bool BeAValidPostcode(string postcode) {
    // custom postcode validating logic goes here
  }
}

Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

bool validationSucceeded = results.IsValid;
IList<ValidationFailure> failures = results.Errors;
公共类CustomerValidator:AbstractValidator{
公共CustomerValidator(){
RuleFor(customer=>customer.姓氏).NotEmpty();
RuleFor(customer=>customer.Forename).NotEmpty().WithMessage(“请指定名字”);
规则(customer=>customer.Discount).NotEqual(0).When(customer=>customer.HasDiscount);
RuleFor(customer=>customer.Address).Length(20250);
RuleFor(customer=>customer.Postcode).Must(BeAValidPostcode).WithMessage(“请指定有效的邮政编码”);
}
私有布尔BeAValidPostcode(字符串邮政编码){
//自定义邮政编码验证逻辑在这里
}
}
客户=新客户();
CustomerValidator validator=新CustomerValidator();
ValidationResult=validator.Validate(客户);
bool validationSucceeded=results.IsValid;
IList failures=结果。错误;

我认为您有些困惑,您没有让您的服务类从抽象验证器继承,您的服务使用一个独立的验证器类。您的意思是“家族”是您想要验证的实体吗?如果是这种情况,您不需要实体从abstractValidator继承,但需要创建一个从abstractValidator继承的验证程序类,该验证程序将把Family实体作为泛型参数传递给abstractValidator。请检查此示例tbat validate customer实体