Default constructor

MicroDal has a buildin mapping function for mapping data from a DbDataReader instance to the selected type. If the type argument has a public parameterless constructor, then MD creates an expression like this to initialize the type from the DbDataReader:
Func<DbDataReader, MyType> map = (r) => new MyType{ MyProp = r["MyProp"] };

It is very important for this to work, that the database query returns a DbDataReader with colums that is named with the propertynames (CASE-INSENSITIVE), and is assignable from the returned db-type.

Type initializer

If the desired return type only have a parameterized constructor, microdal is looking for that constructor with the most parameters and then creates an expression like this to initialize the type from the DbDataReader:
Func<DbDataReader, MyType> map = (r) => new MyType(r["MyParameter"]);


It is very important for this to work, that the database query returns a DbDataReader with colums that is named with the parameternames (CASE-INSENSITIVE), and is assignable from the returned db-type.

Custom initializers

All fetching methods have an override that besides the sql-query also takes an initialization expression of the type Func<DbDataReader, T>. With this you can initialize exactly as you wish. If you have a custom mapper like this, and you use it in a lot of places, you can override the auto-mapping functionality just by calling the SetMapper function on the current DataSession. F.ex. you can provide a custom mapper for returning a single string from each row like this:
MicroDal.DataSession.Current.SetMapper(r => r.GetString(0));

This can be done in the general bootstrapping code in the application (F.ex. in the Global application class in ASP.NET applications).

Last edited Sep 12, 2012 at 7:46 AM by TheIMan, version 2