I am developing a WPF Application using MVVM Architecture. I am an amateur in WPF so bear with me..
I have two model classes. Parent class has an object of another (child) class as its property. (i mean nested objects and not inherited objects)
For instance, 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 validate the properties of Employee entity using Enterprise Library Validation Block.
I was able to do it by implementing IDataErroInfo interface in 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 dont want to implement IDataErroInfo for every child model i create.
Is there any way to validate the Employee object by implementing IDataErrorInfo on the parent (Company) class ?
And also is there any triggers to start validation of objects. I would like to validate the objects only when i want to and not all the time.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…