代码改变世界

微软企业库5.0学习笔记(四十三)数据验证模块

2010-06-19 08:07  Virus-BeautyCode  阅读(3565)  评论(1编辑  收藏  举报

  概况

  任何接受用户或者是其他系统输入的应用,一定要确保信息是合法的,符合特定的规则。例如:在处理一个订单的时候,需要检查客户的电话号码一定要是数字。另外,如果验证失败,需要返回表明错误的信息。

  企业库的验证模块允许开发者实现结构化的,易维护的验证方案。另外,验证模块允许在下面的技术中使用:

  •   ASP.NET
  •   Windows Communication Foundation(WCF)
  •   Windwos Presentation Foundation(WPF)
  •   Windows Forms

  企业库的验证模块是用来验证对象的,不是用来验证UI的控件的。可以验证任何地方的对象。

  你可以用下面的方式定义验证规则,并且实施验证:

  •   在配置文件中为指定的类定义规则集合。
  •   通过在对象的成员上添加attribute。
  •   在代码中添加自验证。
  •   用代码创建validators实例,然后执行验证命令。

  实例代码

 

代码
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public class Customer
{
  [StringLengthValidator(
020)]
  
public string CustomerName;
    
  
public Customer(string customerName)
  {
    
this.CustomerName = customerName;
  }
}

public class MyExample
{
  
private ValidatorFactory factory;

  
public MyExample(ValidatorFactory valFactory)
  {
    factory 
= valFactory;
  }

  
public void MyMethod()
  {
    Customer myCustomer 
= new Customer("A name that is too long");
    Validator
<Customer> customerValidator 
                        
= factory.CreateValidator<Customer>();

    
// Validate the instance to obtain a collection of validation errors.
    ValidationResults r = customerValidator.Validate(myCustomer);
    
if (!r.IsValid)
    {
      
throw new InvalidOperationException("Validation error found.");
    }
  }
}
  

 

  使用验证模块的好处

  •   维护一致的验证体验
  •   可以验证大部分的.NET标准类型。
  •   允许你通过配置、attribute、代码创建验证规则。
  •   允许同一个类关联多个验证规则集合。
  •   在验证对象的时候,可以应用一个或者多个验证规则集合。
  •   可以和ASP.NET,WCF,WPF,WINDOWS FORMS整合。

  添加程序集引用

  添加Microsoft.Practices.EnterpriseLibrary.Validation.dll

  添加

  

  • Microsoft.Practices.EnterpriseLibrary.Common.dll
  • Microsoft.Practices.ServiceLocation.dll
  • Microsoft.Practices.Unity.dll
  • Microsoft.Practices.Unity.Interception.dll

     

      

    • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms.dll
    • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.dll
    • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WPF.dll
    • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF.dll
      using Microsoft.Practices.EnterpriseLibrary.Validation;
      using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

       

        

      

      验证模块的使用

      验证模块包括一个名为validators的类,从validator类继承。还有一个泛型版本validator<T>。

      每一个验证都和一个特定的类型关联。例如:StringLengthValidator类用来检查string是否在预定的长度范围。

      有四种方法可以关联验证类和特定的类型:

    •   配置文件中配置
    •   使用attribute
    •   同时使用配置文件和attribute
    •   使用自验证,在需要验证的对象中包含验证逻辑

      验证类

    •   And Composite Validator

    Class Name: AndCompositeValidator

    Attribute Name: ValidatorCompositionAttribute

    Configuration tool name: And Composite Validator

      

    •   Contains Characters Validator

      

    Class Name: ContainsCharactersValidator

    Attribute Name: ContainsCharactersValidatorAttribute

    Configuration tool name: Contains Characters Validator

    •    Date Time Range Validator
    •   Domain Validator
    •   Enum Conversion Validator
    •   Not Null Validator
    •   Object Collection Validator
    •   Object Validator
    •   Or Composite Validator
    •   Property Comparison Validator
    •   Range Validator
    •   Regular Expression Validator
    •   Relative Date Time Validator
    •   String Length Validator
    •   Type Conversion Validator
    •   Single Member Validators