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 /// <summary>
InBlock.gif
InBlock.gif
/// [url]http://terrylee.cnblogs.com[/url]
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>

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

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

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

ExpandedBlockStart.gif /// <summary>
InBlock.gif
InBlock.gif
/// [url]http://terrylee.cnblogs.com[/url]
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>

None.gif
None.gif
public   class  User
ExpandedBlockStart.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.gif    
{
ExpandedSubBlockStart.gif        
get return _name; }
ExpandedSubBlockStart.gif        
set { _name = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [Int32RangeValidator(
0,200)]
InBlock.gif    
public int Age
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return _age; }
ExpandedSubBlockStart.gif        
set { _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 /// <summary>
InBlock.gif
InBlock.gif
/// [url]http://terrylee.cnblogs.com[/url]
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>

None.gif
None.gif
class  Program
ExpandedBlockStart.gif
{
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.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 /// <summary>
InBlock.gif
InBlock.gif
/// [url]http://terrylee.cnblogs.com[/url]
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>

None.gif
None.gif
class  Program
ExpandedBlockStart.gif
{
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.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.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 /// <summary>
InBlock.gif
InBlock.gif
/// [url]http://terrylee.cnblogs.com[/url]
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>

None.gif
None.gif
class  Program
ExpandedBlockStart.gif
{
InBlock.gif    
static void Main(string[] args)
ExpandedSubBlockStart.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.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就简单得介绍到这儿。









本文转自lihuijun51CTO博客,原文链接:http://blog.51cto.com/terrylee/67649  ,如需转载请自行联系原作者
相关文章