Friday 24 February 2012

Dapper Going Further

In my last post I demonstrated how to use Dapper and showed how to perform basic CRUD operations. Although quicker than using an ORM such as Entity Framework or NHibernate, basic operations need more code along with a deeper understanding of SQL. Luckily there are a number of extensions to Dapper that make our life easier. In this post I’m going to briefly outline what is available and show how one is implemented.

From the creator of Dapper (Sam Saffron) is Dapper.Rainbow (can be installed via Nuget). This is as close to Entity Framework Code First you can get; instead of creating a DbContext you create a Dapper Database class with references to your tables. This is very opinionated and expects the database to be structured in a particular way, so it may cause conflicts with existing setups. Sam has already created an excellent sample of how to use Dapper.Rainbow and it can be found here. In performance terms it is slightly slower than raw Dapper but it is still much quicker than Entity Framework or NHibernate.

Next up again from Sam is an SQL builder for Dapper. Unfortunately this doesn’t have a Nuget package but you can get the source code from the Google code project here and you can drop this file into your solution. If you have to perform a number of dynamic queries and need to build your queries on the fly then this is a powerful solution. If you want more information on using the SQL build you can read this post by Sam as he can explain far better than I ever can!

The next extension library is not from Sam, but from another developer who has taken it upon himself to extend Dapper. The extension is available from Nuget and the source code can be found on GitHub here. The purpose of the library is to keep your POCO clean while giving you a more Entity Framework-style data access for your CRUD operations as well as giving you more advanced query options through the use of predicates. On the project’s GitHub page there are a number of examples that show you how to use the extensions.

The final extension library is the Contrib project within the Dapper project. Again, unfortunately this doesn’t have a Nuget package but the code can be found here and simply dropped into your project. Below is an example of how to perform the same CRUD operations as shown in my last post.





The extension does make some assumptions about your table names and primary keys; these can be specified using the Table attribute and the Key attribute.


As you can see the simple CRUD operations are incredibly simple now using Dapper.Contrib and it does speed up development. As with Dapper.Rainbow, data is expected a certain way and your database might not fit the exact pattern. There are a number of options though - you could modify Dapper.Contrib to suit your needs as it is just a single file, you could use the SQL builder for the more complex examples or you could use Dapper Extensions as this does provide more advanced options.

As mentioned in my last post, Dapper is already used in the wild. If you want to see Dapper.Contrib and the SQL builder in use then have a look at the Stack Exchange Data Explorer - this uses both and the source code is available here.

In my next post in the series I will be looking at how easy it is to convert an existing web application to use Dapper.

No comments:

Post a Comment