注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 IB客座主编(四)美国西蒙公..
 帮助

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


版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terrylee.blog.51cto.com/342737/67649
摘要:在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的工厂,用来创建验证器。首先我们编写一个简单的业务对象类:
/// <summary>

/// http://terrylee.cnblogs.com

/// </summary>


public class User
{
    
private String _name;

    
private int _age;

    
public String Name
    
{
        
get return _name; }
        
set { _name = value; }
    }


    
public int Age
    
{
        
get return _age; }
        
set { _age = value; }
    }

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

/// http://terrylee.cnblogs.com

/// </summary>


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
现在就可以进行验证了,如下面的代码片断所示:
/// <summary>

/// http://terrylee.cnblogs.com

/// </summary>


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,再调用ValidatorValidate方法进行验证,返回的结果ValidationResults是一个ValidationResult的集合,包含了错误信息,我们可以通过KeyMessage属性来显示错误信息,如下所示:
/// <summary>

/// http://terrylee.cnblogs.com

/// </summary>


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。如下面的代码片断所示:
/// <summary>

/// http://terrylee.cnblogs.com

/// </summary>


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就简单得介绍到这儿。

本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67649





    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: