EnterpriseLibrary 3.0的验证应用程序块,可以为系统开发提供统一的验证方法,并且和其他程序块一样操作简单,非常实用。

1,使整个程序维持统一的验证风格;
2,验证对象必须是标准的.net数据类型;
3,允许通过配置文件,特性,代码来创建规则;
4,允许对同一个验证对象设置多个验证规则。

先给大家举个具体操作的例子:

1;向系统添加引用:

EnterpriseLibrary 3.0的验证应用程序块Microsoft.Practices.EnterpriseLibrary.Validation.dll;
EnterpriseLibrary 3.0的验证应用程序块Microsoft.Practices.EnterpriseLibrary.ObjectBuilder.dll
2;根据您的程序类型在下面3个中选择添加引用;
EnterpriseLibrary 3.0的验证应用程序块Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms.dll ;
EnterpriseLibrary 3.0的验证应用程序块Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.dll ;
EnterpriseLibrary 3.0的验证应用程序块Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF.dll ;
3在你的c#代码中使用
EnterpriseLibrary 3.0的验证应用程序块using Microsoft.Practices.EnterpriseLibrary.Validation;
EnterpriseLibrary 3.0的验证应用程序块
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
4,编写代码
EnterpriseLibrary 3.0的验证应用程序块public class Employee
    }

最终显示的结果是:
错误位置:name;原因:The length of the value must fall within the range "0" (Inclusive) - "12" (Inclusive).
错误位置:email;原因:The value must match the regular expression "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" with options "None".
上例中RegexValidator,StringLengthValidator都是validator中的一种验证类型,除此之外validator还有多种验证类型,下面我们来看看如何通过特性和代码来编写验证规则及如何通过 Tag 和 MessageTemplate来关联验证对象。

ContainsCharactersValidator:验证对象是否包含某些特定字符
        属性:CharacterSet:要验证的字符,可以是多个字符组成的字符串;
        ContainsCharacters:有any和all两种,any表示如果CharacterSet中某个字符匹配就满足匹配规则,all表示必须完全匹配;

DateTimeRangeValidator       时间范围验证

DomainValidator        域验证,为验证对象设置指定清单
           eg. [DomainValidator("young", "jiang", "yang")] :验证对象必须是上述列举字符串的一种

EnumConversionValidator       枚举验证
        参数:EnumType
        eg.    enum Sex        {female,male}
              [EnumConversionValidator(typeof(Sex))]

NotNullValidator       非空验证
ObjectValidator       对象验证,将提到的验证器作用与一个对象,如果对象为空,将忽略所有验证,

ObjectCollectionValidator 对象集合验证,参数为指定的集合类型
        eg. [ObjectCollectionValidator(typeof(sex)]

PropertyComparisonValidator       比较验证
        参数:1)比较因子(另一个属性)       2)比较名称 ComparisonOperator,一个枚举
        [PropertyComparisonValidator("MinimumBid", ComparisonOperator.GreaterThanEqual)]
要验证对象必须大于等于MinimumBid属性

RangeValidator       范围验证
eg. [RangeValidator(0, RangeBoundaryType.Inclusive, 110,RangeBoundaryType.Inclusive)]
        验证在0-110之间,包括0和110

RegexValidator       正则验证

RelativeDateTimeValidator       时间范围验证,
我理解与DateTimeRangeValidator的区别是,DateTimeRangeValidator指定具体的时间范围,         RelativeDateTimeValidator与验证对象相关的一个时间范围。
eg.[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year)]

StringLengthValidator        字符串长度验证

TypeConversionValidator 转换验证
eg.   [TypeConversionValidator(typeof(double))]       验证对象需能转换为double型

AndCompositeValidator        and验证       几种验证规则必须全部满足
eg.   [ValidatorComposition(CompositionType.And)]
      [NotNullValidator]
      [StringLengthValidator(10)]
      验证对象必须同时满足NotNullValidator验证和StringLengthValidator验证

OrCompositeValidator        or验证,满足几种验证规则之一即满足验证,可与AndCompositeValidator比较学习

上面列举的所有验证都有一些公共属性来定制验证结果。
MessageTemplate:验证失败后,返回的错误信息。
MessageTemplateResourceName 与 MessageTemplateResourceType从指定模板清单中选择一种模板的名称和类型
Negate:验证取反,bool型,默认为false,不取反
eg. [StringLengthValidator(0,12)]表示验证对象长度在0-12之间,如果在加上Negate=true,表示验证对象长度在0-12之间,则验证失。
Tag :对验证对象分类
Ruleset:为一组规则命名。

实用Self Validation来进行内部验证
用HasSelfValidation 标志需要内部验证的类,实用SelfValidation标注类中需要验证的方法。

EnterpriseLibrary 3.0的验证应用程序块[HasSelfValidation]
EnterpriseLibrary 3.0的验证应用程序块
public class Address
}

在实际程序中根据设置的特性来对对象进行验证

EnterpriseLibrary 3.0的验证应用程序块Customer customer = new Customer();
EnterpriseLibrary 3.0的验证应用程序块ValidationResults results 
= Validation.Validate<Customer>(customer, customerRuleSetCombo.Text);
页可以实用下面代码
EnterpriseLibrary 3.0的验证应用程序块Customer customer = new Customer();
EnterpriseLibrary 3.0的验证应用程序块Validator
<Customer> validator = ValidationFactory.CreateValidator<Customer>(customerRuleSetCombo.Text);
EnterpriseLibrary 3.0的验证应用程序块ValidationResults results 
= validator.Validate(customer);

在asp.net中实用PropertyProxyValidator进行验证
EnterpriseLibrary 3.0的验证应用程序块<asp:TextBox ID="firstNameTextBox" runat="server"></asp:TextBox>
EnterpriseLibrary 3.0的验证应用程序块
<cc1:propertyproxyvalidator id="firstNameValidator"       runat="server"
EnterpriseLibrary 3.0的验证应用程序块          ControlToValidate
="firstNameTextBox"
EnterpriseLibrary 3.0的验证应用程序块          PropertyName
="FirstName"
EnterpriseLibrary 3.0的验证应用程序块          RulesetName
="RuleSetA" 
EnterpriseLibrary 3.0的验证应用程序块          SourceTypeName
="ValidationQuickStart.BusinessEntities.Customer">
EnterpriseLibrary 3.0的验证应用程序块
</cc1:propertyproxyvalidator>

创建型验证规则
EnterpriseLibrary 3.0的验证应用程序块Validator<string> emailAddressValidator = new RegExValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
使用RegExValidator来创建一个新验证器emailAddressValidator
EnterpriseLibrary 3.0的验证应用程序块Validator shortStringValidator = new AndCompositeValidator(
EnterpriseLibrary 3.0的验证应用程序块                                        
new NotNullValidator(),
EnterpriseLibrary 3.0的验证应用程序块                                        
new StringLengthValidator(15));

验证规则继承
EnterpriseLibrary 3.0的验证应用程序块Public Class Customer
}
在上面例子中,PreferredCustomer 继承Customer类,也继承了[CustomerNameValidator]验证规则,但用[PreferredDiscountValidator]取代了 [DiscountValidator]验证规则

相关文章:

  • 2022-01-02
  • 2021-06-04
  • 2021-06-11
  • 2021-12-21
  • 2022-12-23
  • 2021-12-20
  • 2021-10-04
  • 2021-07-03
猜你喜欢
  • 2021-07-02
  • 2022-03-01
  • 2021-06-13
  • 2021-08-02
  • 2021-06-12
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案