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
{
I use ValueInjecter for this
I'm doing like this:
while (dr.Read())
{
var o = new User();
o.InjectFrom(dr);
yield return o;
}
you gonna need this ValueInjection for this to work:
public class DataReaderInjection : KnownSourceValueInjection
{
protected override void Inject(IDataReader source, object target, PropertyDescriptorCollection targetProps)
{
for (var i = 0; i < source.FieldCount; i++)
{
var activeTarget = targetProps.GetByName(source.GetName(i), true);
if (activeTarget == null) continue;
var value = source.GetValue(i);
if (value == DBNull.Value) continue;
activeTarget.SetValue(target, value);
}
}
}