企业库6验证配置文件 [英] Enterprise Library 6 validation config file
问题描述
我正在尝试学习EnterpriseLibraryValidatoin. 当我配置TypeValidation通过配置文件验证类时,它没有启动. 但是当我添加数据注释时,它可以正确验证 我不知道我是否遗漏了一些东西
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
请帮忙
验证配置文件
<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; }
}
推荐答案
每个 Microsoft Enterprise Library 6 –最终版本,发行说明
验证应用程序块
Validation Application Block
ValidationFactory类不再从配置文件自动构建其配置.现在,您必须调用SetDefaultConfigurationValidatorFactory方法以从配置文件中加载配置.这是一个重大变化.
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());
在您进行验证之前.
这篇关于企业库6验证配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!