Wednesday, July 8, 2009

Good Architect for DAL

I came across a very structure way to populate data from database back to object class.

After we retrieve data from database, typically we will have a datareader object. We can leverage on the method below to convert dr value to match with the data type in object class.

protected T GetDataObjectValue(IDataReader dr, string columnName)
{
int i = dr.GetOrdinal(columnName);
if (!dr.IsDBNull(i))
return (T)dr.GetValue(i);
else
return default(T);
}


Next, we can build a function to load the value into object class like below:

obj.ID = base.GetDataValue(dr, "ID");

If required, we can add the object into a object List.

No comments:

Post a Comment