0

我的業務層使用稱爲DomainObject的抽象基類實現IDataErrorInfo,爲WPF提供驗證綁定。 當我調用在基類中實現的「Error」屬性時,沒有發現錯誤(我的測試產生兩個驗證錯誤)。 如果我重寫了派生類中的屬性,所有的東西都如預期的那樣發現並且發現了驗證錯誤。 我的猜測是在「ValidateFromAttributes」方法中反射的問題......?企業庫5.0,抽象基類中的驗證

我的示例應該返回兩個錯誤。

這是我的代碼:

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

回答

1

嘗試使用ValidationFactory代替。例如:

string IDomainObject.Error 
{ 
    get 
    { 
     var v = ValidationFactory.CreateValidator(this.GetType()); 
     var results = v.Validate(this); 
     return string.Join(" ", 
      results.Select(r => r.Message).ToArray()); 
    } 
} 

摘自this blog