如何通过在父类上实现IDataErrorInfo来验证子对象

raj*_*787 3 c# validation wpf mvvm idataerrorinfo

我正在使用MVVM架构开发WPF应用程序.我是WPF的业余爱好者所以请耐心等待..

我有两个模型类.父类具有另一个(子)类的对象作为其属性.(我的意思是嵌套对象而不是继承对象)

例如,请考虑以下方案.

public class Company
{

   public string CompanyName {get; set;}

   public Employee EmployeeObj {get; set;}
}

public class Employee 
{

   public string FirstName {get; set;}

   public string LastName {get; set;}

}    
Run Code Online (Sandbox Code Playgroud)

我想使用Enterprise Library Validation Block验证Employee实体的属性.

我可以通过在employee类中实现IDataErroInfo接口来实现,如下所示

public class Employee :  IDataErrorInfo

{

   [NotNullValidator(MessageTemplate="First Name is mandatory"]
   public string FirstName {get; set;}

   [StringLengthValidator(0,20,MessageTemplate="Invalid")]
   public string LastName {get; set;}

   public string Error
   {
        get
        {
            StringBuilder error = new StringBuilder();

            ValidationResults results = Validation.ValidateFromAttributes<Employee>(this);

            foreach (ValidationResult result in results)
            {
                error.AppendLine(result.Message);
            }

            return error.ToString();
        }

    }

    public string this[string propertyName]
    {
        get
        {
            ValidationResults results = Validation.ValidateFromAttributes<Employee>(this);

            foreach (ValidationResult result in results)
            {
                if (result.Key == propertyName)
                {
                    return result.Message;
                }
            }

            return string.Empty;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我不想为我创建的每个子模型实现IDataErroInfo.

有没有办法通过在父(公司)类上实现IDataErrorInfo来验证Employee对象?

还有任何触发器来启动对象的验证.我想只在我想要而不是所有时间的情况下验证对象.

Ste*_*ven 6

您绝对可以IDataErrorInfo使用Validation Application Block在基类上实现.这是一篇描述如何的文章.代码基本上归结为:

public abstract class DataErrorInfo : IDataErrorInfo
{
    string IDataErrorInfo.Error
    {
        get { return string.Empty; }
    }

    string IDataErrorInfo.this[string columnName]
    {
        get
        {
            var prop = this.GetType().GetProperty(columnName);
            return this.GetErrorInfo(prop); 
        }
    }

    private string GetErrorInfo(PropertyInfo prop)
    {
        var validator = this.GetPropertyValidator(prop);

        if (validator != null)
        {
           var results = validator.Validate(this);

           if (!results.IsValid)
           {
              return string.Join(" ",
                  results.Select(r => r.Message).ToArray());
           }
        }

        return string.Empty;
    }

    private Validator GetPropertyValidator(PropertyInfo prop)
    {
        string ruleset = string.Empty;
        var source = ValidationSpecificationSource.All;
        var builder = new ReflectionMemberValueAccessBuilder();
        return PropertyValidationFactory.GetPropertyValidator(
            this.GetType(), prop, ruleset, source, builder);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以通过继承它来将此抽象类添加验证行为添加到您的实体:

public partial class Customer : DataErrorInfo
{
}
Run Code Online (Sandbox Code Playgroud)