I’m building a program that relies on a old legacy system.
I’m especially writing a custom CRUD repository over a POCO/persistent agnostic model classes.
Ex (simplified):
public class Company { // No dep with the legacy objects
public string CompanyName {get; set;}
}
public class CompanyRepository { // other project
public Company Get(ID companyID)
{
var myOldSchoolCompany = oldSystem.GetCompany(companyID.Key);
return new Company { CompanyName = myOldSchoolCompany.CompanyName; }
}
public Company Save(Company company)
{
var myOldSchoolCompany = oldSystem.GetCompany(companyID.Key);
myOldSchoolCompany.CompanyName = company.CompanyName;
oldSystem.Save(myOldSchoolCompany);
}
}
this code is working as expected, but I’d like to go further, with adding checks and validations.
I need to be able to have mandatory fields, range validation, etc.
I like the DataAnnotation mechanisms that allow me to add this informations on the model itself.
Is it possible (and a good idea) to reuse this mechanisms ?
Precisely, is there a OOB Validate method that can validate a model object ?
thanks in advance,
steve
I think it’s a good practice to use annotations for validation. Some common frameworks like ASP.NET MVC, Entity Framwork make use of this.
You can use Validator class to validate an annotated object.
I recommend you build a small framework to integrate the annotation framework and your system classes.