Enterprise Library v3中的新成员:Validation Application Block

Tom Hollander在他的Blog介绍了作为下一代企业库(开发代号Enterprise Library v3)中的新成员Validation Application BlockTom Hollander在这里提供了一些实现验证的想法,对于Enterprise Library比较关注的朋友可以一睹为快!

定义验证规则

1.使用配置

沿用Enterprise Library中以前的一贯做法,可以使用Enterprise Library配置工具进行配置验证规则。

2.使用特性

通过加上特性来实现验证,如下面的例子所示:

public class Customer
{
    
// Using fields instead of properties for brevity
[NotNullValidator]

    [StringLengthValidator(
150)]
    
public string Name;

    [RelativeDateValidator(RelativeDateKind.Before, OffSetFromNow
=0)]
    
public DateTime DateJoined;
}

3.在代码中验证

某些场景下需要在代码中实现验证

IValidator<string> emailAddressValidator = new RegExValidator("xxxx");
IValidator
<string> shortStringValidator = new AndCompositeValidator<string>(
    
new NotNullValidator<string>(), new StringLengthValidator(15));

在文章中还介绍了验证对象和创建自己的验证器。

详情可以访问:

http://blogs.msdn.com/tomholl/archive/2006/11/27/validation-application-block-revealed.aspx

posted @ 2006-11-28 09:33  TerryLee  阅读(4657)  评论(29编辑  收藏  举报