zoukankan      html  css  js  c++  java
  • EntLib Validation Application Block 01 通过配置文件,自行指定对象自身方法进行验证

    EntLib Index

    通过配置文件,指定该任意公共(形如: CustomValidate(ValidationResults results))

    目标:在领域对象层中,达到不引用 Entlib 的 dll 就可以应用Entlib的验证框架(演示类没有表达出,你可以试试)

    CustomObjectValidator 验证对象

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.EnterpriseLibrary.Validation;
    using System.Reflection;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    
    namespace IIFOR.EntLib
    {
        [ConfigurationElementType(typeof(CustomObjectValidatorData))]
        public class CustomObjectValidator : Validator
        {
            MethodInfo methodInfo;
            string methodName;
            public CustomObjectValidator()
                : this(null, null, null)
            {
            }
    
            public CustomObjectValidator(string methodName, string messageTemplate, string tag)
                : base(messageTemplate, tag)
            {
                this.methodName = methodName;
                if (string.IsNullOrWhiteSpace(this.methodName))
                {
                    this.methodName = "CustomValidate";
                }
            }
    
            protected override string DefaultMessageTemplate
            {
                get { return null; }
            }
    
            public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
            {
                if (null == objectToValidate)
                {
                    this.LogValidationResult(validationResults, "目标为null或引用一个不兼容的类型的一个实例。", currentTarget, key);
                    return;
                }
                if (methodInfo == null)
                {
                    methodInfo = objectToValidate.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                }
                if (methodInfo == null)
                {
                    return;
                }
                else if (!this.methodInfo.DeclaringType.IsAssignableFrom(objectToValidate.GetType()))
                {
                    this.LogValidationResult(validationResults, "目标引用一个不兼容的类型的一个实例。", currentTarget, key);
                }
                else
                {
                    try
                    {
                        this.methodInfo.Invoke(objectToValidate, new object[] { validationResults });
                    }
                    catch (Exception)
                    {
                        this.LogValidationResult(validationResults, "目标调用异常", currentTarget, key);
                    }
                }
            }
        }
    }

    CustomObjectValidatorData config定义的类型

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
    using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
    using System.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Validation;
    
    namespace IIFOR.EntLib
    {
        public class CustomObjectValidatorData : ValueValidatorData
        {
            public CustomObjectValidatorData() { Type = typeof(CustomObjectValidator); }
    
            public CustomObjectValidatorData(string name)
                : base(name, typeof(CustomObjectValidator))
            { }
    
            private const string MethodNamePropertyName = "methodName";
            [ConfigurationProperty(MethodNamePropertyName)]
            public string MethodName
            {
                get { return (string)this[MethodNamePropertyName]; }
                set { this[MethodNamePropertyName] = value; }
            }
    
            protected override Validator DoCreateValidator(Type targetType)
            {
                return new CustomObjectValidator(MethodName, MessageTemplate, Tag);
            }
        }
    }

    ValidateEntityClass 测试对象

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
    using Microsoft.Practices.EnterpriseLibrary.Validation;
    using IIFOR.IBase;
    
    namespace Test.IIFOR.EntLib
    {
        [HasSelfValidation]
        public class ValidateEntityClass
        {
            public ValidateEntityClass()
            {
                this.Code = "Code64649797";
            }
            public string Code { get; set; }
    
    
            public string DName { get; set; }
    
            [SelfValidation(Ruleset = CONSTR.DefaultSetName)]
            public void Validate(ValidationResults results)
            {
                results.AddResult(new ValidationResult("TTT", this, "TTT", "", null));
            }
    
            public void CustomValidate(ValidationResults results)
            {
                results.AddResult(new ValidationResult("CustomValidate", this, "CustomValidate", "", null));
            }
    
            public void CustomValidate1(ValidationResults results)
            {
                results.AddResult(new ValidationResult("CustomValidate1", this, "CustomValidate1", "", null));
            }
        }
    }

    validator.config 配置文件

    <?xml version="1.0"?>
    <validation>
      <type name="Test.IIFOR.EntLib.ValidateEntityClass" assemblyName="Test.IIFOR.EntLib">
        <ruleset name="default">
          <add type="IIFOR.EntLib.CustomObjectValidator, IIFOR.EntLib" name="CustomObjectValidator" />
          <add type="IIFOR.EntLib.CustomObjectValidator, IIFOR.EntLib" name="CustomObjectValidator1" methodName="CustomValidate1" />
          <properties>
            <property name="Code">
              <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation"
                  upperBound="3" name="String Length Validator" />
            </property>
          </properties>
        </ruleset>
      </type>
    </validation>

     

  • 相关阅读:
    Struts2声明式异常处理
    几种常用的过滤器
    Jdk 和 Tomcat的 安装。(旧版本,请看新版本3篇)
    java 判断字符串是否相等
    PreparedStatement 查询 In 语句 setArray 等介绍。
    String、StringBuffer与StringBuilder之间区别
    IntelliJ IDEA 里 查看一个函数注释的方法是 ctrl+q
    Java字符串拼接效率对比
    Java 中判断字符串是否为空
    IntelliJ IDEA + Tomcat ;On Upate Action 与 On Frame Deactivation
  • 原文地址:https://www.cnblogs.com/rock_chen/p/2571073.html
Copyright © 2011-2022 走看看