微软企业库5.0学习笔记实战数据验证模块
2010-06-23 21:21
691 查看
1 在业务对象上添加验证
添加对程序集【Microsoft.Practices.EnterpriseLibrary.Validation.dll】和【System.ComponentModel.DataAnnotations】的引用。
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
定义下面的对象
代码
public class Customer
{
[StringLengthValidator(1, 25)]
public string FirstName { get; set; }
[StringLengthValidator(1, 25)]
public string LastName { get; set; }
[RegexValidator(@"^\d\d\d-\d\d-\d\d\d\d$")]
public string SSN { get; set; }
[ObjectValidator]
public Address Address { get; set; }
}
这时候再次运行程序,发现Address的属性也被验证了。
3 使用配置实现验证
添加需要验证的类,然后为每个类添加验证规则之后,按照上面的图示进行配置(上面只添加了一个类Customer,如果需要用配置验证Address,需要再添加一个Validated Type),去掉业务类上的attribute,再次运行程序。具体的配置步骤和其他模块的配置类似,也可以参考:Microsoft Enterprise Library 5.0 系列(三) Validation Application Block (高级) 。
就会看到下面的结果,配置的验证规则产生了效果,和在对象的属性上添加attribute是一样的效果。
通过代码和配置都可以在一个属性上面同时添加几个验证,例如:同时长度和内容。
[ValidatorComposition(CompositionType.And)]
[StringLengthValidator(1, 50)]
[ContainsCharactersValidator("sea", ContainsCharacters.All)]
public string City { get; set; }
添加对程序集【Microsoft.Practices.EnterpriseLibrary.Validation.dll】和【System.ComponentModel.DataAnnotations】的引用。
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
定义下面的对象
代码
public class Customer
{
[StringLengthValidator(1, 25)]
public string FirstName { get; set; }
[StringLengthValidator(1, 25)]
public string LastName { get; set; }
[RegexValidator(@"^\d\d\d-\d\d-\d\d\d\d$")]
public string SSN { get; set; }
[ObjectValidator]
public Address Address { get; set; }
}
这时候再次运行程序,发现Address的属性也被验证了。
3 使用配置实现验证
添加需要验证的类,然后为每个类添加验证规则之后,按照上面的图示进行配置(上面只添加了一个类Customer,如果需要用配置验证Address,需要再添加一个Validated Type),去掉业务类上的attribute,再次运行程序。具体的配置步骤和其他模块的配置类似,也可以参考:Microsoft Enterprise Library 5.0 系列(三) Validation Application Block (高级) 。
就会看到下面的结果,配置的验证规则产生了效果,和在对象的属性上添加attribute是一样的效果。
通过代码和配置都可以在一个属性上面同时添加几个验证,例如:同时长度和内容。
[ValidatorComposition(CompositionType.And)]
[StringLengthValidator(1, 50)]
[ContainsCharactersValidator("sea", ContainsCharacters.All)]
public string City { get; set; }
相关文章推荐
- 微软企业库5.0学习笔记实战数据验证模块----高级篇
- 微软企业库5.0学习笔记(四十五)实战数据验证模块----高级篇
- 微软企业库5.0学习笔记(四十四)实战数据验证模块
- 微软企业库5.0学习笔记(四十三)数据验证模块(转)
- 微软企业库5.0学习笔记(四十三)数据验证模块
- 微软企业库5.0学习笔记数据验证模块
- 微软企业库5.0学习笔记(三十四)数据访问模块 各种获取数据的方式
- 微软企业库5.0学习笔记(三十三)数据访问模块
- 微软企业库5.0学习笔记(三十四)数据访问模块 各种获取数据的方式
- 微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务
- 微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务
- 微软企业库5.0学习笔记(三十三)数据访问模块
- 微软企业库5.0学习笔记(三十四)数据访问模块 各种获取数据的方式
- 微软企业库5.0学习笔记(三十五)数据访问模块 DataSet以及数据库事务
- 微软企业库5.0学习笔记(三十三)数据访问模块
- 微软企业库4.1学习笔记(三十二)数据访问模块 在应用中使用数据访问模块
- 微软企业库5.0学习笔记(四十二)异常处理模块
- 微软企业库4.1学习笔记(三十一)数据访问模块 简介
- 微软企业库4.1学习笔记(三十一)数据访问模块 简介
- [EntLib]微软企业库5.0 学习之路——第六步、使用Validation模块进行服务器端数据验证