How can I map a DataReader object into a class object by using generics?
For example I need to do the following:
public class Mapper
{
what about following
abstract class DataMapper
{
abstract public object Map(IDataReader);
}
class BookMapper : DataMapper
{
override public object Map(IDataReader reader)
{
///some mapping stuff
return book;
}
}
public class Mapper
{
public static List MapObject(IDataReader dr)
{
List objects = new List();
DataMapper myMapper = getMapperFor(T);
while (dr.Read())
{
objects.Add((T)myMapper(dr));
}
return objects;
}
private DataMapper getMapperFor(T myType)
{
//switch case or if or whatever
...
if(T is Book) return bookMapper;
}
}
Don't know if it is syntactically correct, but I hope u get the idea.