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