Enterprise Library 3.0 (2) Validation Application Block

摘要:在Enterprise Library 3.0 December 2006 CTP版中,加入了一个新的成员Validation Application Block,用来实现对业务对象的验证。它支持两种方式的验证,通过特性Attribute和通过配置文件,但是在最新版本中并没有提供配置的设计时支持,我们只能通过手动去修改配置文件来实现,所以本文主要看一下通过Attribute来实现验证。

[@more@]

主要内容

1.通过ValidationFactory创建验证器

2.通过外观类实现验证

一.概述

Enterprise Library 3.0 December 2006 CTP版中,加入了一个新的成员Validation Application Block,用来实现对业务对象的验证。它支持两种方式的验证,通过特性Attribute和通过配置文件,但是在最新版本中并没有提供配置的设计时支持,我们只能通过手动去修改配置文件来实现,所以本文主要看一下通过Attribute来实现验证。

二.通过ValidationFactory创建验证器

Validation Application Block沿用了其他应用程序块的一贯做法,使用相同的操作模式,为我们提供了一个ValidationFactory的工厂,用来创建验证器。首先我们编写一个简单的业务对象类:

ExpandedBlockStart.gif ContractedBlock.gif /**/ ///
InBlock.gif
InBlock.gif/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif///

None.gif
None.gif public class User
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gifprivate String _name;
InBlock.gif
InBlock.gifprivate int _age;
InBlock.gif
InBlock.gif [NotNullValidator]
InBlock.gif [StringLengthValidator(1,50)]
InBlock.gifpublic String Name
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifget dot.gif{ return _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gifset dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif [Int32RangeValidator(0,200)]
InBlock.gifpublic int Age
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifget dot.gif{ return _age; }
ExpandedSubBlockStart.gifContractedSubBlock.gifset dot.gif{ _age = value; }
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

Validation Application Block中,现在已经提供的验证器有:

l AndCompositeValidator

l Int32RangeValidator

l NotNullValidator

l NullValidator

l OrCompositeValidator

l RangeValidator

l StringLengthValidator

l ValidNumberValidator

l ValueAccessValidator

现在就可以进行验证了,如下面的代码片断所示:

三.通过外观类实现验证

ExpandedBlockStart.gif ContractedBlock.gif /**/ ///
InBlock.gif
InBlock.gif/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif///
None.gif
None.gif class Program
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gifstatic void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif User user = new User();
InBlock.gif
InBlock.gif user.Name = "TerryLee";
InBlock.gif
InBlock.gif user.Age = 60;
InBlock.gif
InBlock.gif IValidator<User> userValidators = ValidationFactory.CreateValidator<User>();
InBlock.gif
InBlock.gif ValidationResults results = userValidators.Validate(user);
InBlock.gif
InBlock.gif Console.WriteLine(results.IsValid.ToString());
InBlock.gif
InBlock.gif Console.Read();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

首先使用ValidationFactory创建Validator,再调用ValidatorValidate方法进行验证,返回的结果ValidationResults是一个ValidationResult的集合,包含了错误信息,我们可以通过KeyMessage属性来显示错误信息,如下所示:

ExpandedBlockStart.gif ContractedBlock.gif /**/ ///
InBlock.gif
InBlock.gif/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif///
None.gif
None.gif class Program
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gifstatic void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif User user = new User();
InBlock.gif
InBlock.gif user.Name = "TerryLee";
InBlock.gif
InBlock.gif user.Age = 210;
InBlock.gif
InBlock.gif IValidator<User> userValidators = ValidationFactory.CreateValidator<User>();
InBlock.gif
InBlock.gif ValidationResults results = userValidators.Validate(user);
InBlock.gif
InBlock.gifforeach (ValidationResult result in results)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Console.WriteLine(String.Format("Key: {0},Message: {1}", result.Key.ToString(), result.Message));
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif Console.Read();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

用过Logging Application Block的朋友都知道,在Logging Application Block中为我们提供了一个Logger的外观类,简化了记录日志。同样在Validation Application Block中,为我们提供了一个Validation的外观类,不需要再使用ValidationFactory。如下面的代码片断所示:

ExpandedBlockStart.gif ContractedBlock.gif /**/ ///
InBlock.gif
InBlock.gif/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif///
None.gif
None.gif class Program
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gifstatic void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif User user = new User();
InBlock.gif
InBlock.gif user.Name = "TerryLee";
InBlock.gif
InBlock.gif user.Age = 210;
InBlock.gif
InBlock.gif ValidationResults results = Validation.Validate<User>(user);
InBlock.gif
InBlock.gifforeach (ValidationResult result in results)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Console.WriteLine(String.Format("Key: {0},Message: {1}", result.Key.ToString(), result.Message));
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif Console.Read();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

可以看到,Validation Application Block沿用了Enterprise Library的一贯操作模式,使用起来也非常的简单。如果提供的验证器不能满足实际开发的需要,也可以很轻松的创建自定义的验证其。关于Validation Application Block就简单得介绍到这儿。

posted on 2006-12-25 23:28 TerryLee 阅读(9412) 评论(21) 编辑 收藏 网摘 所属分类: A

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10771986/viewspace-967737/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10771986/viewspace-967737/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值