黄聪: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. ...
随机推荐
- struts2对action中的方法进行输入校验---xml配置方式(3)
上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍第二种方式:xml配置文件 首先我们来看一个样例: ValidateAction.java: package co ...
- javascript中外部js文件取得自身完整路径得办法
原文:javascript中外部js文件取得自身完整路径得办法 有时候我们需要引入一个外部js文件,这个js文件又需要用到自己的路径或者是所在的目录,别问怎么又这么变态的需求,开发做久了各种奇葩需求也 ...
- Nubia Z5S 基于官方H207/4.4内核的Mokee4.4.4 RC3.2 (2014.7.31修复呼吸灯(能亮依旧不能呼吸))
特别感谢 yun3195 和 轻描淡写Yhw 帮忙測试 转帖请务必注明本链接地址: http://blog.csdn.net/syhost/article/details/36444259 此ROM ...
- js弹出对话框,遮罩效果,
刚刚来到实习单位,我跟着廖哥做项目.然后他分配给我一个小小的任务,实现起来总的效果如下: 然后,但我们单击显示数目这个链接的时候,就会弹出一个又遮罩效果的对话框,如下图: 当我们在对话框中再点击里面的 ...
- 与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏
原文:与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏 [索引页][源码下载] 与众不同 wind ...
- STM32学习笔记2-系统时钟知识及程序配置
一:基本知识 1. STM32F103ZE有5个时钟源:HSI.HSE.LSI.LSE.PLL. ①.HSI是快速内部时钟,RC振荡器,频率为8MHz,精度不高. ②.HSE是快速外部时钟, ...
- web.xml在<init-param>一个错误
Description ResourcePathLocationType cvc-complex-type.2.4.a: Invalid content was found starting with ...
- 利用手工编码的方式对srtus2进行输入验证
对action方法进行校验有两种方法一种是: 1手工编码书写 2一种是用xml 输入校验的流程: 1类型转化器对请求参数执行类型转化,并把转换后的值赋给action属性. 2.如果执行类型转化的过程中 ...
- Enthought科学计算,数据分析
Enthought Canopy: Easy Python Deployment Plus Integrated Analysis Environment for Scientific Computi ...
- How-To: add EPEL repository to Centos 6.x is Easy!
How-To: add EPEL repository to Centos 6.x is Easy! | ITek Blog How-To: add EPEL repository to Centos ...