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

     

  • 相关阅读:
    Tomcatd断点调试Debug
    idea怎么部署Servlet
    ECMAScript基本语法——①与HTML的结合方式
    JavaScript简介
    程序员找工作,应该怎么应对面试官?
    你所未知的3种 Node.js 代码优化方式
    对 APM 用户的一次真实调查分析(上)
    Datadog Agent是啥?它消耗什么资源?
    Python 全栈开发 -- 开发环境篇
    成为运维界的「福尔摩斯」,你还需要3个帮手!
  • 原文地址:https://www.cnblogs.com/rock_chen/p/2571073.html
Copyright © 2020-2023  润新知