Sam Saffron wrote the micro-ORM Dapper and Rob Connery wrote the micro-ORM massive. Both ORMs are written in less than 400 lines of C# code.
What is interesting to me is how they both used constructs borrowed from Dynamic Languages to write their ORM and making the same point I made by writing DynamicSugar.net
Both ORM use
- Anonymous Type to pass nicely a series of name/value.
- The keyword dynamic and the class Expando to deal with the passed anonymous types as well as the accessing the data returned by SELECT statments
Massive Sample:
// var dog = connection.ExecuteMapperQuery<dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid }); //Massive Sample:
// var newID = table.Insert(new {CategoryName = "Buck Fify Stuff", Description = "Things I like"}); //
In Massive code, the anonymous types are converted into dictionaries using reflection like in DynamicSugar.net.
Dapper is about performance and therefore does not use reflection to access the properties of the anonymous types. Instead cached Dynamic Methods containing specific generated IL
allow to extract the information. That sounds complex, but it is not to bad.
See file SqlMapper.cs method CreateParamInfoGenerator().
For DynamicSugar.net I like to get rid of reflection to access anonymous types, in the same way. But before using IL, I tried using Expression Trees which I got working, and then I discovered FastReflection which provided exactly what I needed including the caching. I did have a problem with a property containing a generic type and therefore I decided to keep reflection for the first version.
No comments:
Post a Comment