BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

StackOverflow’s ORM goes Open Source - Dapper.Net

by David Cooksey on Apr 14, 2011 |

A simple ORM used in StackOverflow titled Dapper.Net was recently released on code.google.com. 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.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Meh by N T

Don't make me laugh. An ORM that only supports Linq to SQL and is only tested against a couple of MSSQL servers. Where do I sign up? It sounds more like a helper class to me.

Re: Meh by Robert Sullivan

Yes, "meh", unless you are dealing with the sort of traffic SO gets. Obviously not everyone out there is dealing with the performance issues that SO, Facebook, Twitter, and Google are. I don't know about SO, but Twitter's one billion queries per day sets it apart from your average user site. It's pretty typical for companies to tune their hardware and software for their own requirements, and these may not be yours.

Re: Meh by Lars Corneliussen

+1 for Helper Class.

This is not even close to a mapper between object-oriented an relatio al paradogms.

Re: Meh by Stefan Wenig

LINQ to SQL... minus the "LINQ to" part

drapper.net is not an ORM by Stefan Wenig

they use the word object mapper. what they do is fast loading of result sets into objects. a very thin layer above direct acccess of result sets, but useful if you can't spare any CPU cycles.

yet another data mapper by Binoj Antony

Nice one, looking at this I am encouraged to convert my own mapper (xmldatamapper.codeplex.com/) to use Reflection.Emit.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

6 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT