Enterprise Library 6验证配置文件

时间:2013-09-27 08:41:55

标签: validation enterprise-library-6

我正在尝试学习EnterpriseLibraryValidatoin。 当我配置TypeValidation以通过配置文件验证类时,它不会提取。 但是当我添加数据注释时,它会正确验证 我不知道我是否要留下一些东西

任何帮助,请

验证配置文件

<validation>
<type name="ValidationBlockExample.Person" defaultRuleset="ValidimiFushave"
    assemblyName="ValidationBlockExample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
    <ruleset name="ValidimiFushave">
        <fields>
            <field name="LastName">
                <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    messageTemplate="Last Name Required!" name="Not Null Validator" />
            </field>
        </fields>
    </ruleset>
</type>

验证代码

        ValidationFactory.SetDefaultConfigurationValidatorFactory(new SystemConfigurationSource(false));

        Validator<Person> pValidator = ValidationFactory.CreateValidator<Person>();

        Person prsn = new Person();
        prsn.FirstName = "Name";
        ////prsn.LastName = "Haz";
        prsn.Age = 31;

        ValidationResults valResults = pValidator.Validate(prsn);


        if (!valResults.IsValid)
        {
            foreach (var valResult in valResults)
            {
                Console.WriteLine(valResult.Message);

            }

        }
        else
            Console.WriteLine("Preson Validated !!!");

        Console.ReadLine();

要验证的课程

public class Person
{
    public string FirstName { get; set; }
    //[Required]
    public string LastName { get; set; }
    public int Age { get; set; }

}

1 个答案:

答案 0 :(得分:3)

Microsoft Enterprise Library 6 – Final Release, Release Notes

  

验证申请阻止

     

ValidationFactory类不再自动从配置文件构建其配置。您现在必须调用SetDefaultConfigurationValidatorFactory方法从配置文件加载配置。这是一个重大改变。

所以,正如我所建议的那样,这样做:

ValidationFactory.SetDefaultConfigurationValidatorFactory(
new SystemConfigurationSource());
验证前