• 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>

     

  • 相关阅读:
    用 Java 通过串口发送手机短信(二)简单使用
    jquery.lazyload.js实现图片延迟加载——wordpress图片随滚动条渐显效果
    java的mysql连接池
    html5客户端本地存储之sessionStorage及storage事件
    flash chart(amCharts的破解)
    实现igoogle效果的两种方法比较
    “SQL Server (SQL2005) 服务因 126 (0x7E) 服务性错误而停止”的解决方法 
    一个超简单的方法验证文本框中内容的数据类型
    我的对联
    Ajax调用后类方法时报”对象未定义“错误解决方法
  • 原文地址:https://www.cnblogs.com/rock_chen/p/2571073.html
Copyright © 2020-2023  润新知