Hello,
My data access classes often have a method called loadDataToEntity or something similar. This method takes a resultset for instance and populates an entity class which is passed on to be used by the client.
This is nice because all methods which use a stored procedure to retrieve data can just pass the resultset to the loadDataToEntity method and get back an entity.
So it's a good way to reduce code but I often hear that you should retrieve as few columns as possible. To use my general method all stored procedures needs to fetch all columns needed for loadDataToEntity(). Also, when a column is added to the table and is retrieved by a stored procedure all other stored procedures needs to be altered and also retrieve that column.
I'm wondering if there's a better way to both achieve a general method and also retrieve different number of columns sometimes depending on the stored procedure?
public Entity getById(int id)
{
ResultSet rs;
//call stored procedure A and stuff
return loadDataToEntity(rs);
}
public Entity getByName(String name)
{
ResultSet rs;
//call stored procedure B and stuff
return loadDataToEntity(rs);
}
public Entity loadDataToEntity(ResultSet rs)
{
// fill and return entity here
}