微软企业库5.0学习笔记(四十四)实战数据验证模块

  1 在业务对象上添加验证

  添加对程序集【Microsoft.Practices.EnterpriseLibrary.Validation.dll】和【System.ComponentModel.DataAnnotations】的引用。

  using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

  定义下面的对象

  

ExpandedBlockStart.gif 代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> public   class  Customer
    {
        [StringLengthValidator(
1 25 )]
        
public   string  FirstName {  get set ; }
        [StringLengthValidator(
1 25 )]
        
public   string  LastName {  get set ; }
        [RegexValidator(
@" ^\d\d\d-\d\d-\d\d\d\d$ " )]
        
public   string  SSN {  get set ; }
       
        
public  Address Address {  get set ; }
    }
    
public   class  Address
    {
        [StringLengthValidator(
1 50 )]
        
public   string  StreetAddress {  get set ; }
        [ValidatorComposition(CompositionType.And)]
        [StringLengthValidator(
1 50 )]
        [ContainsCharactersValidator(
" sea " , ContainsCharacters.All)]
        
public   string  City {  get set ; }
        [StringLengthValidator(
2 2 )]
        
public   string  State {  get set ; }
        [RegexValidator(
@" ^\d{5}$ " )]
        
public   string  ZipCode {  get set ; }
    }

 

  2 对业务对象进行验证

  添加对

  Microsoft.Practices.EnterpriseLibrary.Validation.dll

  Microsoft.Practices.ServiceLocation.dll

  Microsoft.Practices.EnterpriseLibrary.Common.dll

  的引用。

  

ExpandedBlockStart.gif 代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Globalization;
using  Microsoft.Practices.EnterpriseLibrary.Validation;
using  Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using  Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using  Entity;

namespace  AOPValidation.ConApp
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Validator
< Customer >  customerValidator  =   null ;
            ValidatorFactory validatorFactory 
=  EnterpriseLibraryContainer.Current.GetInstance < ValidatorFactory > ();
            customerValidator 
=  validatorFactory.CreateValidator < Customer > ();

            Customer customer 
=   new  Customer()
            {
                FirstName 
=   " 1231231233333333333333333333333333333333333333 " ,
                LastName 
=   " 2342424234 " ,
                SSN 
=   " sdfsd " ,
                Address 
=   new  Address()
                {
                    City 
=   " sdf " ,
                    State 
=   " dsf " ,
                    StreetAddress 
=   " sdfsdf " ,
                    ZipCode 
=   " sd "
                }
            };
           ValidationResults validationResults
=  customerValidator.Validate(customer);
           
if  ( ! validationResults.IsValid)
           {
               Console.WriteLine(
" customer is invalid " );
               StringBuilder sb 
=   new  StringBuilder( 0 );
               
foreach  (ValidationResult result  in  validationResults)
               {
                   sb.AppendLine(
string .Format(CultureInfo.CurrentCulture,  " {0}:{1} " ,
                       result.Key, result.Message));
               }
               Console.WriteLine(sb.ToString());
           }
           
else
           {
               Console.WriteLine(
" customer is valid " );
           }
           Console.ReadLine();
        }
    }
}

 

  验证的结果显示如下

  

  你会发现只是验证了Customer对象的属性,Address上面也添加了验证attribute,可是没有起到效果。对象里面的属性经常会是另外一个对象的,难道要将对象的所有对象属性都定义一个validator,然后validate一下吗,好像有点麻烦, 有没有解决办法呢?
  有的,只要在Customer的Address属性上面添加一个attribute,【 ObjectValidator】,是的Customer变为下面的形式即可。
  
ExpandedBlockStart.gif 代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->    public   class  Customer
    {
        [StringLengthValidator(
1 25 )]
        
public   string  FirstName {  get set ; }
        [StringLengthValidator(
1 25 )]
        
public   string  LastName {  get set ; }
        [RegexValidator(
@" ^\d\d\d-\d\d-\d\d\d\d$ " )]
        
public   string  SSN {  get set ; }
        [ObjectValidator]
        
public  Address Address {  get set ; }
    }

 

   这时候再次运行程序,发现Address的属性也被验证了。

  

 

  3 使用配置实现验证

  

 

  添加需要验证的类,然后为每个类添加验证规则之后,按照上面的图示进行配置(上面只添加了一个类Customer,如果需要用配置验证Address,需要再添加一个Validated Type),去掉业务类上的attribute,再次运行程序。具体的配置步骤和其他模块的配置类似,也可以参考:Microsoft Enterprise Library 5.0 系列(三) Validation Application Block (高级)

  

  就会看到下面的结果,配置的验证规则产生了效果,和在对象的属性上添加attribute是一样的效果。

  

  通过代码和配置都可以在一个属性上面同时添加几个验证,例如:同时长度和内容。

 

 

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->  [ValidatorComposition(CompositionType.And)]
   [StringLengthValidator(
1 50 )]
   [ContainsCharactersValidator(
" sea " , ContainsCharacters.All)]
   
public   string  City {  get set ; }

 

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值