Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)


企业库提供了一个很强大的验证应用程序模块,特点是:

  • 可以通过配置为你的程序中特定的类来定义规则集.
  • 是为你的类的公有属性,即对外开放的属性进行验证的.

使用企业库验证应用程序模块的优势:

  • 有助于保持一致的验证方法。
  • 包括大多数标准验证,包括.NET数据类型校验.
  • 它让您可以将多个规则集具有相同的类和该类的成员.
  • 它可以让你申请一个或多个规则集时,您验证的对象.

企业库验证应用程序模块提供了下列几种验证方法:

  • And CompositeValidator
  • ContainsCharacters Validator
  • Date Time RangeValidator
  • Domain Validator
  • Enum ConversionValidator
  • Not Null Validator
  • Object CollectionValidator
  • Object Validator
  • Or CompositeValidator
  • PropertyComparison Validator
  • Range Validator
  • Regular ExpressionValidator
  • Relative Date TimeValidator
  • String LengthValidator
  • Type ConversionValidator
  • Single MemberValidators

企业库验证应用程序模块有2种使用模式:

  1. 代码模式.
  2. 配置文件模式.

本文讲的是代码模式,配置文件模式在高级篇再介绍

下面介绍如何使用Microsoft Enterprise Library 5.0中的验证应用程序模块的代码模式.

  1. 要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是Microsoft.Practices.EnterpriseLibrary. Validation.dll ,System.ComponentModel.DataAnnotations.dll ,并添加需要的引用:

 Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)

添加引用:

usingMicrosoft.Practices.EnterpriseLibrary.Validation.Validators;
usingMicrosoft.Practices.EnterpriseLibrary.Validation;
usingSystem.Collections.Generic;

  2. 测试:

Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)代码

  3. 运行结果:

Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)




本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/archive/2010/05/30/1747612.html,如需转载请自行联系原作者