微软企业库5.0学习笔记(四十三)数据验证模块
2010-06-19 08:07 Virus-BeautyCode 阅读(3568) 评论(1) 编辑 收藏 举报概况
任何接受用户或者是其他系统输入的应用,一定要确保信息是合法的,符合特定的规则。例如:在处理一个订单的时候,需要检查客户的电话号码一定要是数字。另外,如果验证失败,需要返回表明错误的信息。
企业库的验证模块允许开发者实现结构化的,易维护的验证方案。另外,验证模块允许在下面的技术中使用:
- ASP.NET
- Windows Communication Foundation(WCF)
- Windwos Presentation Foundation(WPF)
- Windows Forms
企业库的验证模块是用来验证对象的,不是用来验证UI的控件的。可以验证任何地方的对象。
你可以用下面的方式定义验证规则,并且实施验证:
- 在配置文件中为指定的类定义规则集合。
- 通过在对象的成员上添加attribute。
- 在代码中添加自验证。
- 用代码创建validators实例,然后执行验证命令。
实例代码

using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public class Customer
{
[StringLengthValidator(0, 20)]
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.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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构