当前位置: 移动技术网 > IT编程>开发语言>.net > .NET验证组件Fluent Validation使用指南

.NET验证组件Fluent Validation使用指南

2017年12月12日  | 移动技术网IT编程  | 我要评论

易才招聘网,南昌新闻网,辛巴达历险记第一季

认识fluent vaidation.

  看到nopcommerce项目中用到这个组建是如此的简单,将数据验证从业务实体类中分离出来,真是一个天才的想法,后来才知道这个东西是一个开源的轻量级验证组建。
 
  fluent validation 翻译为:流畅验证
 
  开源codeplex其主页简介:该组件是一个轻量级的.net类库,使用流畅的接口定义和lambda表达式为构建一个业务类的验证规则(a small validation library for .net that uses a fluent interface and lambda expression for building validation rules for you business objects.)
 
  这个类库不仅仅可以使用的asp.net mvc项目中,普通的类库中也可以使用,当然在asp.net form项目中也支持。

怎么使用:  
  是不是好用,还要看使用时是否真的像其官网建议描述一样。我比较喜欢其官网上的例子,一眼就能看出用法上的感觉,绝对是如其名,流畅,这个也一种解释型语言常见的的一种用法,无限的对一个类型支持无限度个属性扩展。

业务实体类:
 

复制代码 代码如下:

 public class person
 {
     public string namefield;
     public int id { get; set; }
     public string surname { get; set; }
     public string forename { get; set; }
     public list<person> children { get; set; }
     public string[] nicknames { get; set; }
     public datetime dateofbirth { get; set; }
     public int? nullableint { get; set; }
     public person()
     {
         children = new list<person>();
         orders = new list<order>();
     }
     public int calculatesalary()
     {
         return 20;
     }
     public address address { get; set; }
     public ilist<order> orders { get; set; }
     public string email { get; set; }
     public decimal discount { get; set; }
     public double age { get; set; }
     public int anotherint { get; set; }
     public string creditcard { get; set; }
     public int? othernullableint { get; set; }
 }
 public interface iaddress
 {
     string line1 { get; set; }
     string line2 { get; set; }
     string town { get; set; }
     string county { get; set; }
     string postcode { get; set; }
     country country { get; set; }
 }
 public class address : iaddress
 {
     public string line1 { get; set; }
     public string line2 { get; set; }
     public string town { get; set; }
     public string county { get; set; }
     public string postcode { get; set; }
     public country country { get; set; }
     public int id { get; set; }
 }
 public class country
 {
     public string name { get; set; }
 }
 public interface iorder
 {
     decimal amount { get; }
 }
 public class order : iorder
 {
     public string productname { get; set; }
     public decimal amount { get; set; }
 }

对person的指定验证规则:  
 

复制代码 代码如下:

 using fluentvalidation;
 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;

flent validation怎么与asp.net mvc验证库整合?
  如果在asp.net mvc中现实中这么用,可能会有很多人不会知道他,我们知道asp.net mvc项目中有自己的验证机构[企业库vab(validation application block),基于attribute声明式验证],其使用方法,也被我们都一直很认可,但其也有很多不够灵活的,但fluent validation确实更灵活一点。使用起来多变性,流畅,而且验证规则是一个单独的类,是和业务实体对象分类的,我们不需要翔vab一样,需要在业务实体类上使用attribute注册验证规则。
 
  既然其不是asp.net mvc的默认验证规则类库,我们就需要注册到asp.net mvc的验证规则库中。
 

复制代码 代码如下:

 // 在global.asax.cs中的applicaton_start()函数中注册为asp.net mvc默认的验证规则库。
 // fluent validation
 fluentvalidationmodelvalidatorprovider provider = new fluentvalidationmodelvalidatorprovider(new attributedvalidatorfactory());
 modelvalidatorproviders.providers.add(provider);
 dataannotationsmodelvalidatorprovider.addimplicitrequiredattributeforvaluetypes = false;

注意:
  1,)作为fluent validation验证规则类须继承abstractvalidator<t>;
 
  2,)我们也可以仿照nopcommerce的处理方法,对attributevalidatorfactory类的validator(type type)函数重写,在特殊的业务环境下支持其他验证规则。

本文适合对.net以及mvc有所了解的读者,这里抛砖引玉,献丑了

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网