Enterprise Library 6 validation config file

∥☆過路亽.° 提交于 2019-12-23 00:51:18

问题


i'm trying to learn EnterpriseLibraryValidatoin. when i configure TypeValidation to validate a class through config file it does not pick up. but when i add Data Annotations it Validates Correctly I don't know if i'm leaving something out

any help please

validation config file

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

code to validate

        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();

the class to be validated

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

}

回答1:


Per Microsoft Enterprise Library 6 – Final Release, Release Notes

Validation Application Block

The ValidationFactory class no longer automatically builds its configuration from the configuration file. You must now invoke the SetDefaultConfigurationValidatorFactory method to load the configuration from the configuration file. This is a breaking change.

So, as has been suggested to me, do this:

ValidationFactory.SetDefaultConfigurationValidatorFactory(
new SystemConfigurationSource());

before you validate.



来源:https://stackoverflow.com/questions/19046427/enterprise-library-6-validation-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!