Dapper - a simple object mapper for .Net
Located at https://github.com/DapperLib/Dapper/releases
MyGet Pre-release feed: https://www.myget.org/gallery/dapper
Package NuGet Stable NuGet Pre-release Downloads MyGet Dapper Dapper.EntityFramework Dapper.EntityFramework.StrongName Dapper.Rainbow Dapper.SqlBuilder Dapper.StrongNamePackage Purposes:
Dapper was originally developed for and by Stack Overflow, but is F/OSS. Sponsorship is welcome and invited - see the sponsor link at the top of the page. A huge thanks to everyone (individuals or organisations) who have sponsored Dapper, but a massive thanks in particular to:
Dapper is a NuGet library that you can add in to your project that will enhance your ADO.NET connections via
extension methods on your DbConnection instance. This provides a simple and efficient API for invoking SQL, with support for both synchronous and
asynchronous data access, and allows both buffered and non-buffered queries.
It provides multiple helpers, but the key APIs are:
// insert/update/delete etc
var count = connection.Execute(sql [, args]);
// multi-row query
IEnumerable<T> rows = connection.Query<T>(sql [, args]);
// single-row query ({Single|First}[OrDefault])
T row = connection.QuerySingle<T>(sql [, args]);
where args can be (among other things):
Dictionary<string,object>DynamicParameters instancepublic class Dog
{
public int? Age { get; set; }
public Guid Id { get; set; }
public string Name { get; set; }
public float? Weight { get; set; }
public int IgnoredProperty { get { return 1; } }
}
var guid = Guid.NewGuid();
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });
Assert.Equal(1,dog.Count());
Assert.Null(dog.First().Age);
Assert.Equal(guid, dog.First().Id);
This method will execute SQL and return a dynamic list.
Example usage:
var rows = connection.Query("select 1 A, 2 B union all select 3, 4").AsList();
Assert.Equal(1, (int)rows[0].A);
Assert.Equal(2, (int)rows[0].B);
Assert.Equal(3, (int)rows[1].A);
Assert.Equal(4, (int)rows[1].B);
Example usage:
var count = connection.Execute(@"
set nocount on
create table #t(i int)
set nocount off
insert #t
select @a a union all select @b
set nocount on
drop table #t", new {a=1, b=2 });
Assert.Equal(2, count);
The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)
Example usage:
var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
);
Assert.Equal(3, count); // 3 rows inserted: "1,1", "2,2" and "3,3"
Another example usage when you already have an existing collection:
var foos = new List<Foo>
{
{ new Foo { A = 1, B = 1 } }
{ new Foo { A = 2, B = 2 } }
{ new Foo { A = 3, B = 3 } }
};
var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", foos);
Assert.Equal(foos.Count, count);
This works for any parameter that implements IEnumerable<T> for some T.
A key feature of Dapper is performance. The following metrics show how long it takes to execute a SELECT statement against a DB (in various config, each labeled) and map the data returned to objects.
The benchmarks can be found in Dapper.Tests.Performance (contributions welcome!) and can be run via:
dotnet run --project .\benchmarks\Dapper.Tests.Performance\ -c Release -f net8.0 -- -f * --join
Output from the latest run is:
BenchmarkDotNet v0.13.7, Windows 10 (10.0.19045.3693/22H2/2022Update)
Intel Core i7-3630QM CPU 2.40GHz (Ivy Bridge), 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.100
[Host] : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX
ShortRun : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX
ORM
Method
Return
Mean
StdDev
Error
Gen0
Gen1
Gen2
Allocated
Dapper cache impact
ExecuteParameters_Cache
Void
96.75 us
0.668 us
1.010 us
0.6250
-
-
2184 B
Dapper cache impact
QueryFirstParameters_Cache
Void
96.86 us
0.493 us
0.746 us
0.8750
-
-
2824 B
Hand Coded
SqlCommand
Post
119.70 us
0.706 us
1.067 us
1.3750
1.0000
0.1250
7584 B
Hand Coded
DataTable
dynamic
126.64 us
1.239 us
1.873 us
3.0000
-
-
9576 B
SqlMarshal
SqlCommand
Post
132.36 us
1.008 us
1.523 us
2.0000
1.0000
0.2500
11529 B
Dapper
QueryFirstOrDefault
Post
133.73 us
1.301 us
2.186 us
1.7500
1.5000
-
11608 B
Mighty
Query
dynamic
133.92 us
1.075 us
1.806 us
2.0000
1.7500
-
12710 B
LINQ to DB
Query
Post
134.24 us
1.068 us
1.614 us
1.7500
1.2500
-
10904 B
RepoDB
ExecuteQuery
Post
135.83 us
1.839 us
3.091 us
1.7500
1.5000
-
11649 B
Dapper
'Query (buffered)'
Post
136.14 us
1.755 us
2.653 us
2.0000
1.5000
-
11888 B
Mighty
Query
Post
137.96 us
1.485 us
2.244 us
2.2500
1.2500
-
12201 B
Dapper
QueryFirstOrDefault
dynamic
139.04 us
1.507 us
2.279 us
3.5000
-
-
11648 B
Mighty
SingleFromQuery
dynamic
139.74 us
2.521 us
3.811 us
2.0000
1.7500
-
12710 B
Dapper
'Query (buffered)'
dynamic
140.13 us
1.382 us
2.090 us
2.0000
1.5000
-
11968 B
ServiceStack
SingleById
Post
140.76 us
1.147 us
2.192 us
2.5000
1.2500
0.2500
15248 B
Dapper
'Contrib Get'
Post
141.09 us
1.394 us
2.108 us
2.0000
1.5000
-
12440 B
Mighty
SingleFromQuery
Post
141.17 us
1.941 us
2.935 us
1.7500
1.5000
-
12201 B
Massive
'Query (dynamic)'
dynamic
142.01 us
4.957 us
7.494 us
2.0000
1.5000
-
12342 B
LINQ to DB
'First (Compiled)'
Post
144.59 us
1.295 us
1.958 us
1.7500
1.5000
-
12128 B
RepoDB
QueryField
Post
148.31 us
1.742 us
2.633 us
2.0000
1.5000
0.5000
13938 B
Norm
'Read<> (tuples)'
ValueTuple`8
148.58 us
2.172 us
3.283 us
2.0000
1.7500
-
12745 B
Norm
'Read<()> (named tuples)'
ValueTuple`8
150.60 us
0.658 us
1.106 us
2.2500
2.0000
1.2500
14562 B
RepoDB
Query
Post
152.34 us
2.164 us
3.271 us
2.2500
1.5000
0.2500
14106 B
RepoDB
QueryDynamic
Post
154.15 us
4.108 us
6.210 us
2.2500
1.7500
0.5000
13930 B
RepoDB
QueryWhere
Post
155.90 us
1.953 us
3.282 us
2.5000
0.5000
-
14858 B
Dapper cache impact
ExecuteNoParameters_NoCache
Void
162.35 us
1.584 us
2.394 us
-
-
-
760 B
Dapper cache impact
ExecuteNoParameters_Cache
Void
162.42 us
2.740 us
4.142 us
-
-
-
760 B
Dapper cache impact
QueryFirstNoParameters_Cache
Void
164.35 us
1.206 us
1.824 us
0.2500
-
-
1520 B
DevExpress.XPO
FindObject
Post
165.87 us
1.012 us
1.934 us
8.5000
-
-
28099 B
Dapper cache impact
QueryFirstNoParameters_NoCache
Void
173.87 us
1.17