原文:黄聪: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 ,并添加需要的引用:

添加引用:

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

  2. 测试:

代码

using System;

using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using System.Collections.Generic;

namespace test
{
class Program
{
staticint index =;

staticvoid Main(string[] args)
{
//验证Customer类
Validator<Customer> customerValidator = ValidationFactory.CreateValidator<Customer>();

//设置Customer的CustomerName字段为null
Customer myCustomer =new Customer(null);

ValidationResults vr = customerValidator.Validate(myCustomer);
Scan(vr);

//设置Customer的CustomerName
myCustomer.CustomerName ="HuangCong";

vr = customerValidator.Validate(myCustomer);
Scan(vr);

//创建一个日期
DateTime dt =new DateTime(, , );

//创建一个日期验证器
Validator<DateTime> v1 =new DateTimeRangeValidator(DateTime.Parse("2009-01-01"), DateTime.Parse("2010-01-01"));
vr = v1.Validate(dt);
Scan(vr);

dt =new DateTime(, , );
vr = v1.Validate(dt);
Scan(vr);

/*
其他的验证类还有如下这些,大家可以自己实验:

And Composite Validator
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

参考网站:http://msdn.microsoft.com/en-us/library/ff664694%28v=PandP.50%29.aspx
*/
}

publicclass Customer
{
//Not Null Validator 验证器,验证该属性不能为空值
[NotNullValidator]
publicstring CustomerName;

public Customer(string customerName)
{
this.CustomerName = customerName;
}
}

privatestaticvoid Scan(ValidationResults vr)
{
Console.WriteLine("测试{0}:", index++);
if (!vr.IsValid)
{
Console.WriteLine("出错");
}
else
{
Console.WriteLine("正确");
}
Console.WriteLine("---------------------------------------");
}
}
}

  3. 运行结果:

黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)的更多相关文章

  1. 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级) 企业库验证应用程序模块之配置文件模式: ...

  2. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 企业库加密应用程序模块提供了2种方 ...

  3. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...

  4. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  5. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图:   从上图我们可以 ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...

  7. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block 到目前为止,我们使用的模块都是在同一个配置 ...

  9. Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    Download dll: http://www.microsoft.com/en-us/download/confirmation.aspx?id=15104 http://www.cnblogs. ...

随机推荐

  1. 一劳永逸解决UAC问题(修改QMAKE_LFLAGS_EXE的设置)

    如果你的程序跑在一个开启了UAC保护的系统中,而你的程序又没有"盾牌"的话,程序总是会受到各种阻挠的,比如读写文件,写注册表等. 有了"盾牌"的话就不会出现一些 ...

  2. JavaScript + CSS3 实现的海报画廊特效

    原文:JavaScript + CSS3 实现的海报画廊特效 这是慕课网上<CSS3+JS 实现超炫的散列画廊特效>的源代码,我修改了一些 bug 和调优了一些细节,并把学习过程中并不了解 ...

  3. android onKeydown

    package wyf.ytl; import android.app.Activity; import android.content.Context; import android.os.Bund ...

  4. VMware vSphere 服务器虚拟化之二十二桌面虚拟化之创建View Composer链接克隆的虚拟桌面池

    VMware vSphere 服务器虚拟化之二十二桌面虚拟化之创建View Composer链接克隆的虚拟桌面池 在上一节我们创建了完整克隆的自动专有桌面池,在创建过程比较缓慢,这次我们将学习创建Vi ...

  5. POJ 2184 Cow Exhibition 01背包

    题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负   且 sum(x[k]) >= 0 sum(y[k] ...

  6. Android读取网络图片

    本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在android4.0之后,已不同意在主线程中进行网络请求操作了, 否则会出现N ...

  7. Swift - 文件,文件夹操作大全

    ios开发经常会遇到读文件,写文件等,对文件和文件夹的操作,这时就可以使用NSFileManager,NSFileHandle等类来实现. 下面总结了各种常用的操作: 1,遍历一个目录下的所有文件 1 ...

  8. Swift编程语言学习9—— 存储属性和计算属性

    属性将值跟特定的类.结构或枚举关联.存储属性存储常量或变量作为实例的一部分,计算属性计算(而不是存储)一个值.计算属性能够用于类.结构体和枚举里,存储属性仅仅能用于类和结构体. 存储属性和计算属性通经 ...

  9. MySQL如何修改root密码

    MySQL修改用户密码         因为长期不登录MySQL数据库,登录时经常忘记root权限密码.本文提供一个在数据库服务器上修改root密码的方法,本文撰写基础是在xp操作系统下进行. 第一步 ...

  10. Referer反反盗链

    0x00 前言 最近用Python非常多,确实感受到了Python的强大与便利.但同时我并没有相见恨晚的感觉,相反我很庆幸自己没有太早接触到Python,而是基本按着C→C++→Java→Python ...