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(th开发者_开发百科is);
// 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);
}
Try using the ValidationFactory
instead. For instance:
string IDomainObject.Error
{
get
{
var v = ValidationFactory.CreateValidator(this.GetType());
var results = v.Validate(this);
return string.Join(" ",
results.Select(r => r.Message).ToArray());
}
}
Taken from this blog.
精彩评论