InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

StackOverflow’s ORM goes Open Source - Dapper.Net

Posted by David Cooksey on Apr 14, 2011

Sections
Operations & Infrastructure,
Architecture & Design,
Development
Topics
Data Access ,
.NET ,
Programming ,
ORM ,
Stack Overflow ,
Database ,
Micro-ORM

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.

Meh by N T Posted
Re: Meh by Robert Sullivan Posted
Re: Meh by Lars Corneliussen Posted
Re: Meh by Stefan Wenig Posted
drapper.net is not an ORM by Stefan Wenig Posted
yet another data mapper by Binoj Antony Posted
  1. Back to top

    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.

  2. Back to top

    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.

  3. Back to top

    Re: Meh

    by Lars Corneliussen

    +1 for Helper Class.

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

  4. Back to top

    Re: Meh

    by Stefan Wenig

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

  5. Back to top

    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.

  6. Back to top

    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.