I have a 2 classes:
- Employee
- EmployeeDetails
I have methods like this for Employee class:
public static Employee LoadEmployee(int id)
{
StringBuilder selectQuery = new StringBuilder();
string selectQuery = " my query with the parameter 'Id' ";
dbGenerator _db = dbGenerator.Instance.Database;
DbCommand selectCommand = _db.GetSqlStringCommand(selectQuery.ToString());
_db.AddInParameter(selectCommand, "@id", System.Data.DbType.String, Id);
//EXECUTE THE COMMAND
using (IDataReader dr = _db.ExecuteReader(selectCommand))
{
if (dr.Read())
{
result = new Employee();
Employee.LoadDataReader(result, dr);
}
dr.Close();
}
return result;
}
And I have a method doing the same thing with the EmployeeDetails class … no change at all except the return type.
I am sure there are ways to minimize the lines of code and also using the C# generics it will more readable and generics provide type safety without the overhead of multiple implementations.
I need a method in which I can pass the class object, selectQuery, ParameterId like this
public static <T> LoadData<T>(Object object, string selectQuery, int paramId)
{
// do stuff
// return <T>results;
}
Any suggestion would be appreciated.
The others’ suggestions of just using an ORM are great, but if you have a good reason for doing this layer from scratch, you could amend your design to look something like this:
The main constraint is that you need to have a no-args constructor on your entity objects, otherwise the
LoadEmployeemethods can’t create them correctly. That is, unless you explicitly pass a factory object as a parameter to the method.