How to check child objects by implementing IDataErrorInfo in parent class

I am developing a WPF application using MVVM Architecture. I am a fan of WPF, so bear with me ..

I have two model classes. A parent class has an object of another (child) class as its property. (I mean nested objects and non-inheritable objects)

For example, consider the following scenario.

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

I want to check the properties of an Employee object using the Enterprise Library Health Checker.

I managed to do this by implementing the IDataErroInfo interface in the employee class, as shown below

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

I do not want to implement IDataErroInfo for every child model that I create.

Is there a way to test an Employee object by implementing IDataErrorInfo in the parent (Company) class?

There are also triggers to start checking objects. I would like to check objects only when I want and not all the time.

+2
source share
1 answer

You can absolutely implement IDataErrorInfo in the base class using the validation application block. Here is an article that describes how to do this. The code basically boils down to the following:

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

You can use this abstract class to add a behavior check to your objects, inheriting from it:

 public partial class Customer : DataErrorInfo { } 
+6
source

Source: https://habr.com/ru/post/1369639/


All Articles