forked from jonwagner/Insight.Database
-
Notifications
You must be signed in to change notification settings - Fork 0
FastExpando and Mutations
Jon Wagner edited this page Apr 25, 2012
·
3 revisions
Sometimes you have an ugly database that you aren't allowed to modify. It may be ugly, AND you aren't allowed to create new views or stored procedures to make it look nice. But you want your code to look nice, right?
You have two choices:
- Make a class, bind the data to private members and expose pretty properties.
- You don't want a class, so use FastExpando.
The FastExpando supports two methods to help your code look nicer - Mutate and Transform. Both of them accept a map (a dictionary of string to string), and convert the field names into new field names.
-
expando.Mutate(map)
- modifies the currentexpando
in-place -
dynamic newexpando = expando.Transform(map)
- transforms an expando into a new expando
Usually you will have the mapping as a static dictionary that is reused across calls.
In all cases, unmapped properties are retained as-is.
NOTE: if you map two source properties to the same target value, the behavior is undefined.
var mapping = new Dictionary<string, string>() { { "Name", "TheName" } };
dynamic beer = Database.Connection().QuerySql("SELECT * FROM Beer WHERE Name = @Name", new { Name = "IPA" }).First();
beer.Mutate(mapping);
Console.WriteLine(beer.TheName);
dynamic beer = Database.Connection().QuerySql("SELECT * FROM Beer WHERE Name = @Name", new { Name = "IPA" }).First().Transform(mapping);
Console.WriteLine(beer.TheName);
IEnumerable also supports transforming the objects while enumerating:
foreach (dynamic beer in Database.Connection().QuerySql("SELECT * FROM Beer WHERE Name = @Name", new { Name = "IPA" }).Transform(mapping))
Console.WriteLine(beer.TheName);
- Home
- About
- Getting Started
- Connections
- Execute
- Getting Results
- Advanced Results & Mapping
- Insert/Update Considerations
- Data Types
- Write Even Less Code
- Performance & Speed
- Other Topics
- Supported Databases
- Working with the Code