Enterprise Library 3.0 体验(2):使用Validation Application Block

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

 

主要内容

1.通过ValidationFactory创建验证器

2.通过外观类实现验证

 

一.概述

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

二.通过ValidationFactory创建验证器

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

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

None.gif
None.gif
public   class  User
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private String _name;
InBlock.gif
InBlock.gif    
private int _age;
InBlock.gif
InBlock.gif    
public String Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _age; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _age = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

这只是一个普通的业务实体类,现在我们要验证它的姓名属性不能为空,且长度在150之间,年龄字段在0200之间,加上如下Attribute

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
InBlock.gif
/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
public   class  User
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private String _name;
InBlock.gif
InBlock.gif    
private int _age;
InBlock.gif
InBlock.gif    [NotNullValidator]
InBlock.gif    [StringLengthValidator(
1,50)]
InBlock.gif    
public String Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [Int32RangeValidator(
0,200)]
InBlock.gif    
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _age; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set 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 /**/ /// <summary>
InBlock.gif
InBlock.gif
/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
class  Program
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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 /**/ /// <summary>
InBlock.gif
InBlock.gif
/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
class  Program
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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.gif        
foreach (ValidationResult result in results)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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 /**/ /// <summary>
InBlock.gif
InBlock.gif
/// http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
class  Program
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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.gif        
foreach (ValidationResult result in results)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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就简单得介绍到这儿。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值