zoukankan      html  css  js  c++  java
  • 微软企业库5.0学习笔记(九)

    企业库提供了一个很强大的验证应用程序模块,特点是:

    • 可以通过配置为你的程序中特定的类来定义规则集.
    • 是为你的类的公有属性,即对外开放的属性进行验证的.

     

    使用企业库验证应用程序模块的优势:

    • 有助于保持一致的验证方法。
    • 包括大多数标准验证,包括.NET数据类型校验.
    • 它让您可以将多个规则集具有相同的类和该类的成员.
    • 它可以让你申请一个或多个规则集时,您验证的对象.

    企业库验证应用程序模块提供了下列几种验证方法:

    • And CompositeValidator
    • ContainsCharacters Validator
    • Date Time RangeValidator
    • Domain Validator
    • Enum ConversionValidator
    • Not Null Validator
    • Object CollectionValidator
    • Object Validator
    • Or CompositeValidator
    • PropertyComparison Validator
    • Range Validator
    • Regular ExpressionValidator
    • Relative Date TimeValidator
    • String LengthValidator
    • Type ConversionValidator
    • Single MemberValidators

    企业库验证应用程序模块有2种使用模式:

    1. 代码模式.
    2. 配置文件模式.

    本文讲的是代码模式,配置文件模式在高级篇再介绍

    下面介绍如何使用Microsoft Enterprise Library 5.0中的验证应用程序模块的代码模式.

    ProjectDemo截图:

    1. 要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是Microsoft.Practices.EnterpriseLibrary. Validation.dll ,System.ComponentModel.DataAnnotations.dll ,并添加需要的引用:

     

     

    添加引用:

     

    usingMicrosoft.Practices.EnterpriseLibrary.Validation.Validators;
    usingMicrosoft.Practices.EnterpriseLibrary.Validation;
    usingSystem.Collections.Generic;

     

      2. 测试:

     

    代码
    using System;

    using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
    using Microsoft.Practices.EnterpriseLibrary.Validation;
    using System.Collections.Generic;

    namespace test
    {
    class Program
    {
    static int index = 1;

    static void Main(string[] args)
    {
    //验证Customer类
    Validator<Customer> customerValidator = ValidationFactory.CreateValidator<Customer>();

    //设置Customer的CustomerName字段为null
    Customer myCustomer = new Customer(null);

    ValidationResults vr
    = customerValidator.Validate(myCustomer);
    Scan(vr);

    //设置Customer的CustomerName
    myCustomer.CustomerName = "HuangCong";

    vr
    = customerValidator.Validate(myCustomer);
    Scan(vr);

    //创建一个日期
    DateTime dt = new DateTime(1988, 01, 01);

    //创建一个日期验证器
    Validator<DateTime> v1 = new DateTimeRangeValidator(DateTime.Parse("2009-01-01"), DateTime.Parse("2010-01-01"));
    vr
    = v1.Validate(dt);
    Scan(vr);

    dt
    = new DateTime(2009, 5, 5);
    vr
    = v1.Validate(dt);
    Scan(vr);

    /*
    其他的验证类还有如下这些,大家可以自己实验:

    And Composite Validator
    Contains Characters Validator
    Date Time Range Validator
    Domain Validator
    Enum Conversion Validator
    Not Null Validator
    Object Collection Validator
    Object Validator
    Or Composite Validator
    Property Comparison Validator
    Range Validator
    Regular Expression Validator
    Relative Date Time Validator
    String Length Validator
    Type Conversion Validator
    Single Member Validators

    参考网站:
    http://msdn.microsoft.com/en-us/library/ff664694%28v=PandP.50%29.aspx
    */
    }

    public class Customer
    {
    //Not Null Validator 验证器,验证该属性不能为空值
    [NotNullValidator]
    public string CustomerName;

    public Customer(string customerName)
    {
    this.CustomerName = customerName;
    }
    }

    private static void Scan(ValidationResults vr)
    {
    Console.WriteLine(
    "测试{0}:", index++);
    if (!vr.IsValid)
    {
    Console.WriteLine(
    "出错");
    }
    else
    {
    Console.WriteLine(
    "正确");
    }
    Console.WriteLine(
    "---------------------------------------");
    }
    }
    }

      3. 运行结果:

     

    http://www.cnblogs.com/huangcong/archive/2010/05/30/1747612.html 

  • 相关阅读:
    视频会议保险行业解决方案
    如何选择适合企业的音视频解决方案?
    移动视频会议市场
    网络视频通讯服务器和平台发展
    移动互联网化的远程医疗
    智能家居行业的音视频技术
    Ubuntu15.04 安装Android开发环境
    AspNetPager 自定义html
    SqlDataReader对象的NextResult方法读取存储过程多个结果集
    一些原生云APP
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1749562.html
Copyright © 2011-2022 走看看