摘要:在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的工厂,用来创建验证器。首先我们编写一个简单的业务对象类:

/**//// 
/// http://terrylee.cnblogs.com
/// 
public class User

{
private String _name;
private int _age;
[NotNullValidator]
[StringLengthValidator(1,50)]
public String Name
{
get
{ return _name; }
set
{ _name = value; }
}
[Int32RangeValidator(0,200)]
public int Age
{
get
{ return _age; }
set
{ _age = value; }
}
}在Validation Application Block中,现在已经提供的验证器有:
l AndCompositeValidator
l Int32RangeValidator
l NotNullValidator
l NullValidator
l OrCompositeValidator
l RangeValidator
l StringLengthValidator
l ValidNumberValidator
l ValueAccessValidator
现在就可以进行验证了,如下面的代码片断所示:
三.通过外观类实现验证

/**//// 
/// http://terrylee.cnblogs.com
/// 
class Program

{
static void Main(string[] args)
{
User user = new User();
user.Name = "TerryLee";
user.Age = 60;
IValidator<User> userValidators = ValidationFactory.CreateValidator<User>();
ValidationResults results = userValidators.Validate(user);
Console.WriteLine(results.IsValid.ToString());
Console.Read();
}
}首先使用ValidationFactory创建Validator,再调用Validator的Validate方法进行验证,返回的结果ValidationResults是一个ValidationResult的集合,包含了错误信息,我们可以通过Key和Message属性来显示错误信息,如下所示:

/**//// 
/// http://terrylee.cnblogs.com
/// 
class Program

{
static void Main(string[] args)
{
User user = new User();
user.Name = "TerryLee";
user.Age = 210;
IValidator<User> userValidators = ValidationFactory.CreateValidator<User>();
ValidationResults results = userValidators.Validate(user);
foreach (ValidationResult result in results)
{
Console.WriteLine(String.Format("Key: {0},Message: {1}", result.Key.ToString(), result.Message));
}
Console.Read();
}
}用过Logging Application Block的朋友都知道,在Logging Application Block中为我们提供了一个Logger的外观类,简化了记录日志。同样在Validation Application Block中,为我们提供了一个Validation的外观类,不需要再使用ValidationFactory。如下面的代码片断所示:

/**//// 
/// http://terrylee.cnblogs.com
/// 
class Program

{
static void Main(string[] args)
{
User user = new User();
user.Name = "TerryLee";
user.Age = 210;
ValidationResults results = Validation.Validate<User>(user);
foreach (ValidationResult result in results)

{
Console.WriteLine(String.Format("Key: {0},Message: {1}", result.Key.ToString(), result.Message));
}
Console.Read();
}
}可以看到,Validation Application Block沿用了Enterprise Library的一贯操作模式,使用起来也非常的简单。如果提供的验证器不能满足实际开发的需要,也可以很轻松的创建自定义的验证其。关于Validation Application Block就简单得介绍到这儿。
posted on 2006-12-25 23:28 TerryLee 阅读(9412) 评论(21) 编辑 收藏 网摘 所属分类: A
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10771986/viewspace-967737/ ,如需转载,请注明出处,否则将追究法律责任。