mardi 17 février 2015

Best way to implement validation in a class?

I have a design issue in my application. I have a class with fields having some kind of business logic validations. This class is instantiated and filled with data in two ways.. 1st: By consumer of the class: Data sent from Front end and populated into the object and saved in Database. 2nd: While fetching the stored data. In 2nd requirement I have a problem. I don't want to validate data since it may be possible the data stored may not comply to the business logic due to deletion or modification in existing data. So I need to populate the object with saved data without validation.


so kindly suggest me best way to add validation logic in a class, so that when it is used for data save then it should be validated, and when fetching data it should not validate any of the fields if the key field exists in database table. Eg:



class customer
{
private string customerCode;
private string customerName;
private List<Project> projectList;
//These property contains validation logic for data assigned to them.
public string CustomerCode{get; set;}
public string CustomerName{get; set;}
public List<Project> projectList{get;set;};

public bool SetData(ref string message)
{
//Fetch From Database and set it to fields.
//Here to avoid validation I can use fields directly to skip validation.
this.CustomerCode = DataTable[CustomerCode];
this.CustomerName = DataTable[CustomerName];

//But here its not possbible to skip validation in Project class
foreach(projectID in DataTable[Projects])
{
//**Problem Area**.... every project I add is validated according to business logic, but it may be possible that even after completion of a project users of the system want to list all the projects of that customer.
this.ProjectList.Add(new Project(projectID));
}
}
}

Aucun commentaire:

Enregistrer un commentaire