BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

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

StackOverflow’s ORM goes Open Source - Dapper.Net

Bookmarks

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.

Rate this Article

Adoption
Style

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.

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

Community comments

  • Meh

    by N T,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    +1 for Helper Class.

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

  • Re: Meh

    by Stefan Wenig,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • drapper.net is not an ORM

    by Stefan Wenig,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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

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

BT