Dapper is a micro-ORM for .NET developed and used by the Stack Overflow team, focusing on raw performance as the primary aim.
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. Instead, it focuses on making the materialization as fast as possible, with no overheads from things like identity managers - just "run this query and give me the (typed) data".
However, it retains support for materializing non-trivial data structures, with both horizontal (joined data in a single grid) and vertical (multiple grids) processing.
Quite possibly the fastest materializer available for .NET, and available here: github.com/StackExchange/dapper-dot-net
Getting Started with Dapper
Dapper comprises of a single file: SqlMapper.cs (or SqlMapperAsync.cs for .NET 4.5 if you like async). You can either include the file, as is, in your project. Or install it via nuget.
Here is a video with a simple example.
A simple Hello World sample
using Dapper;
//...
using (var connection = new SqlConnection(myConnectionString))
{
connection.Open();
var posts = connection.Query<Post>("select * from Posts");
//... do stuff with posts
}
CRUD operations
Dapper provides a minimal interface between your database and your application. Even though the interface is minimal it still supports the full array of database operations. Create, Read, Update, Delete are fully supported. Using stored procedures or not.
See also:
- Performing Inserts and Updates with Dapper
- Inserting an IEnumerable<T> collection with Dapper errors out with "class is not supported by Dapper."
- Is there a way to call a stored procedure with Dapper?
The Multi Mapper
Dapper allows you to automatically split a single row to multiple objects. This comes in handy when you need to join tables.
See also:
- Can't get multi-mapping to work in Dapper
- dapper -multi-mapping: flat sql return to nested objects
- How do I select an aggregate object efficiently using Dapper?
Performance
A key feature of Dapper is performance. You can go through the performance of SELECT mapping over 500 iterations at this link.
.NET Framework support
Dapper runs best on .NET 4.0 or later. It makes use of Dynamic features in the language and optional params. You can run Dapper on .NET 3.5 as well. There are no known ports that avoid dynamic method generation. There are no known ports to .NET 2.0.
Dapper also works fine in Mono.
Nuget package
Dapper can most easily be installed through its NuGet package.
Install-Package Dapper
Install
Dapper can be used after adding the Reference of of Dapper.dll
to the project
Extensions
The Dapper solution includes the following extensions:
- Dapper.Rainbow
- Dapper.Contrib
- Dapper.SqlBuilder
3rd party extensions
There are some extensions available, developed by 3rd parties: