Enterprise Library 5.0,抽象基类中的验证
我的业务层使用一个名为 DomainObject 的抽象基类,它实现 IDataErrorInfo 来为 WPF 提供验证绑定。 当我调用基类中实现的“Error”属性时,没有发现错误(我的测试产生两个验证错误)。 如果我重写派生类中的属性,一切都会按预期工作,并且会发现验证错误。 我的猜测是“ValidateFromAttributes”方法中的反射有问题......?
我的示例应该返回两个错误。
这是我的代码:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using ValidationResult =
Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult;
public interface IDomainObject
{
string Name { set; get; }
}
public abstract class DomainObject<T>
: IDataErrorInfo where T : IDomainObject
{
protected DomainObject()
{
}
protected DomainObject(EntityObject entityObject)
{
_entityObject = entityObject;
}
public string this[string columnName]
{
get
{
ValidationResults results = Validation.ValidateFromAttributes(this);
foreach (ValidationResult result in results)
{
if (result.Key == columnName)
{
return result.Message;
}
}
return string.Empty;
}
}
// *** This dosen't work and returns NO errors
public virtual string Error
{
get
{
StringBuilder error = new StringBuilder();
ValidationResults results = Validation.ValidateFromAttributes(this);
foreach (ValidationResult result in results)
{
error.AppendLine(result.Message);
}
return error.ToString();
}
}
private EntityObject _entityObject;
internal EntityObject Entity
{
get { return _entityObject; }
set { _entityObject = value; }
}
}
[HasSelfValidation]
public class BoInvestment : DomainObject<BoInvestment>,
IDomainObject
{
public BoInvestment(){}
internal BoInvestment(EntityObject entityObject) : base(entityObject) {}
[Required]
[StringLengthValidator(7,
MessageTemplate = "Name is too long")]
public string Name { get; set; }
[SelfValidation]
public void Validate(ValidationResults validationResults)
{
if (Name != "Test")
validationResults.AddResult(new ValidationResult(
"Name ist falsch",this,"Name",null,null));
}
// *** This works and returns two errors
//public override string Error
//{
// get
// {
// StringBuilder error = new StringBuilder();
// ValidationResults results = Validation.ValidateFromAttributes(this);
// foreach (ValidationResult result in results)
// {
// error.AppendLine(result.Message);
// }
// return error.ToString();
// }
//}
}
这是单元测试:
[TestMethod]
public void ELValidation()
{
BoInvestment investment = new BoInvestment();
investment.Name = "TestError";
Console.WriteLine(investment.Error);
}
my business layer uses an abstract base class called DomainObject that implements IDataErrorInfo to offer validation-binding for WPF.
When I call the "Error" property implemented in the base class, no errors are found (my test produces two validation-errors).
If I override the property in the derived class everything woks as expected and validation errors are found.
My guess is a problem with reflection in the "ValidateFromAttributes" method...?
My sample should return two errors.
This is my Code:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using ValidationResult =
Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult;
public interface IDomainObject
{
string Name { set; get; }
}
public abstract class DomainObject<T>
: IDataErrorInfo where T : IDomainObject
{
protected DomainObject()
{
}
protected DomainObject(EntityObject entityObject)
{
_entityObject = entityObject;
}
public string this[string columnName]
{
get
{
ValidationResults results = Validation.ValidateFromAttributes(this);
foreach (ValidationResult result in results)
{
if (result.Key == columnName)
{
return result.Message;
}
}
return string.Empty;
}
}
// *** This dosen't work and returns NO errors
public virtual string Error
{
get
{
StringBuilder error = new StringBuilder();
ValidationResults results = Validation.ValidateFromAttributes(this);
foreach (ValidationResult result in results)
{
error.AppendLine(result.Message);
}
return error.ToString();
}
}
private EntityObject _entityObject;
internal EntityObject Entity
{
get { return _entityObject; }
set { _entityObject = value; }
}
}
[HasSelfValidation]
public class BoInvestment : DomainObject<BoInvestment>,
IDomainObject
{
public BoInvestment(){}
internal BoInvestment(EntityObject entityObject) : base(entityObject) {}
[Required]
[StringLengthValidator(7,
MessageTemplate = "Name is too long")]
public string Name { get; set; }
[SelfValidation]
public void Validate(ValidationResults validationResults)
{
if (Name != "Test")
validationResults.AddResult(new ValidationResult(
"Name ist falsch",this,"Name",null,null));
}
// *** This works and returns two errors
//public override string Error
//{
// get
// {
// StringBuilder error = new StringBuilder();
// ValidationResults results = Validation.ValidateFromAttributes(this);
// foreach (ValidationResult result in results)
// {
// error.AppendLine(result.Message);
// }
// return error.ToString();
// }
//}
}
This is the Unit Test:
[TestMethod]
public void ELValidation()
{
BoInvestment investment = new BoInvestment();
investment.Name = "TestError";
Console.WriteLine(investment.Error);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试改用
ValidationFactory
。例如:取自此博客。
Try using the
ValidationFactory
instead. For instance:Taken from this blog.