Basic CRUD operation using Dapper!

Written on Dec 23, 2014

Dapper is an object mapper for .Net. As per stackoverflow definition, dapper is a micro-ORM, offering core parameterization and materialization services, but (by design) not the full breadth of services that you might expect in a full ORM such as LINQ-to-SQL or Entity Framework.

While working on one of project in my company, I have come across this library and found that it is one of the fastest ORM available in .net. For statistics, below is the result of select mapping over 500 iterations using different methods.

Method Duration
Hand coded (using a SqlDataReader) 47ms
Dapper ExecuteMapperQuery 49ms
ServiceStack.OrmLite (QueryById) 50ms
PetaPoco 52ms
BLToolkit 80ms
SubSonic CodingHorror 107ms
NHibernate SQL 104ms
Linq 2 SQL ExecuteQuery 181ms
Entity framework ExecuteStoreQuery 631ms

You can check more performance results of dapper at this guthub link.

Let’s create sample CRUD operation project with dapper as an ORM using ASP.NET MVC 4 and Sql server.

You can add dapper library to your project by downloading it from google code, or by using NuGet packages as shown in below image.

Dapper Nuget Package

I have created class named person as shown below.

Also created database as shown below.

Dapper Database

Below are the basic CRUD operations mapping with dapper.

  1. Select operation

  1. Directly executing sql statement

  1. Using stored procedures and pass argument

  1. Using dynamic parameters to pass arguments

  1. Returning output parameter from stored procedure

Hope, this article is helpful to you for getting started with dapper.