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.
source share