黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)
原文:黄聪: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种使用模式:
- 代码模式.
- 配置文件模式.
本文讲的是代码模式,配置文件模式在高级篇再介绍
下面介绍如何使用Microsoft Enterprise Library 5.0中的验证应用程序模块的代码模式.
- 要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是Microsoft.Practices.EnterpriseLibrary. Validation.dll ,System.ComponentModel.DataAnnotations.dll ,并添加需要的引用:

添加引用:
usingMicrosoft.Practices.EnterpriseLibrary.Validation.Validators;usingMicrosoft.Practices.EnterpriseLibrary.Validation;usingSystem.Collections.Generic;
2. 测试:
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 (初级)的更多相关文章
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级) 企业库验证应用程序模块之配置文件模式: ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 企业库加密应用程序模块提供了2种方 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图: 从上图我们可以 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block 到目前为止,我们使用的模块都是在同一个配置 ...
- Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
Download dll: http://www.microsoft.com/en-us/download/confirmation.aspx?id=15104 http://www.cnblogs. ...
随机推荐
- Oracle静态监听与动态监听概念全解析
基于11g,linux5.5做出的测试,单实例数据库做出的测试. 1.注册 Instance到监听器去注册自己的Instance_name与ORACLE_HOME,还可以选择添加global_dbna ...
- MFC消息映射的原理:笔记
多态的实现机制有两种,一是通过查找绝对位置表,二是查找名称表:两者各有优缺点,那么为什么mfc的消息映射采用了第二种方法,而不是c++使用的第一种呢?因为在mfc的gui类库是一个庞大的继承体系,而里 ...
- 无法安装vmware tools的解决方PLEASE WAIT! VMware Tools is currently being installed on your system. Dependin
VMware安装unbuntu 12.04 LTS时,当你使用VMware的Easy Mode安装时,提示须要安装VMware Tools,屏幕会出现下方的文字: installed unbuntu ...
- ExtJs4 笔记(3) Ext.Ajax 对ajax的支持
本篇主要介绍一下ExtJs常用的几个对JS语法的扩展支持,包括Ajax封装,函数事件操作封装,还有扩展的常用函数等.Ajax服务端交互式操作是提交到.NET MVC.后续服务端交互都采用这一方式实现. ...
- android自定义实现抽屉SlidingDrawer的功能
最近项目中需要实现上拉功能,首先想到的就是Android本身自带的抽屉SlidingDrawer,最后也实现了不过,出现的问题就是设置背景色问题,handler和content是两个不同的部分,这就造 ...
- asp.net 下载任意格式文件 上传文件后台代码
思路:将文件转化为流,输出到页面上的iframe中去 //下载附件逻辑 object DownLoad(NameValueCollection nv) { int attachId = nv[&quo ...
- Spring4 MVC 多文件上传(图片并展示)
开始需要在pom.xml加入几个jar,分别是 <dependency> <groupId>commons-fileupload</groupId> <art ...
- sql server 2012 数据库还原方法
USE master RESTORE DATABASE WSS_Content FROM DISK = N'D:\bak\contentbak.bak' WITH REPLACE, NORECOVER ...
- [Cocos2d-x]Android的android.mk文件通用版本
原文地址: http://blog.ready4go.com/blog/2013/10/12/update-android-dot-mk-with-local-src-files-and-local- ...
- SLB 权重问题
<pre name="code" class="html">一般配置SLB的时候有个权重0到100,是如何选择数值的? 权重需要您根据后端机器的配置 ...