Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News StackOverflow’s ORM goes Open Source - Dapper.Net

StackOverflow’s ORM goes Open Source - Dapper.Net

A simple ORM used in StackOverflow titled Dapper.Net was recently released on This ORM specializes in fast generation of objects from SQL query results. Dapper.Net supports executing sql queries and mapping their results to a strongly typed list or a list of dynamic objects. The ORM is a single file of less than 500 lines of C# code and is available under the Apache 2.0 License.

Why create Dapper.Net when there are so many other ORMS available? Sam Saffron, a senior developer at StackOverflow, explained that StackOverflow relies heavily on simple clustered-index based queries that take an insignificant amount of time on the database server. However, the web tier often reached 100% CPU due to the volume of queries. With hundreds of calls per second, the CPU overhead from LINQ to SQL constructing a dynamic method for every call placed too much of a burden on the web servers. To resolve this problem, Dapper.Net caches information about every query. This comprehensive caching helps it to generate objects from queries about twice as fast as LINQ to SQL. Currently caching is handled by two ConcurrentDictionary objects, which are never cleared. In the future the caching algorithm may use a LRU cache to reduce memory pressure.

Dapper.Net adds two mapping functions via extension methods to the IDbConnection interface, both named ExecuteMapperQuery. The first maps results into a strongly typed list while the second maps results into a list of dynamic objects. ExecuteMapperCommand executes and returns no result set. All three methods accept parameters as anonymous classes, where a property value is mapped to the SQL parameter of the same name. For example, the following code block assigns 5 to @Id and “01234567890” to @acctNum. Any fields returned by the query that are not on the mapped class are silently discarded.

List<Customer> customers = conn.ExecuteMapperQuery<Customer>("SELECT * FROM Sales.Customer WHERE CustomerId=@Id OR AccountNumber=@acctNum", new { Id=5, AcctNum="0123456789"});

Dapper.Net is intended to handle only result-set-to-object mapping. It does not handle relations between objects and it does not generate SQL queries of any kind automatically. Dapper.Net also assumes that the connection is open and ready.

Rate this Article