Are there any quick way to map some IDataReader to object without third party libraries such as AutoMapper or ValueInjecter?
Mapping IDataReader to object without third party libraries
668 Views Asked by Saw At
2
Are there any quick way to map some IDataReader to object without third party libraries such as AutoMapper or ValueInjecter?
I'm not sure what you mean by quick, but you can put something together using reflection. There's a lot of assumptions you'll have to make, such as all your object's values are set via properties. And, your DataReader columns MUST match your object property name. But you could do something like this:
NOTE: The
SetProperty
function is from an article on DevX. (It was in VB.NET, and I converted it to C# -- if there are mistakes, I probably missed something.)No guarantees that this code will run (I'm just typing it and not compiling/testing it), but it at least may be a start.
I've done things like this in the past and have gotten fancy with applying attributes to my properties stating what data reader column to map to the property. Too much to include here, but this is just a starter and hopefully is what you're looking for.
Hope this helps!