Skip to content

Latest commit

History

1,233 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Dapper - a simple object mapper for .Net

Build status

Release Notes

Located at stackexchange.github.io/Dapper

Features

Dapper is a NuGet library that you can add in to your project that will extend your IDbConnection interface.

It provides 3 helpers:

Execute a query and map the results to a strongly typed List

publicstaticIEnumerable<T>Query<T>(thisIDbConnectioncnn,stringsql,objectparam=null,SqlTransactiontransaction=null,boolbuffered=true)

Example usage:

publicclassDog{publicint?Age{get;set;}publicGuidId{get;set;}publicstringName{get;set;}publicfloat?Weight{get;set;}publicintIgnoredProperty{get{return1;}}}varguid=Guid.NewGuid();vardog=connection.Query<Dog>("select Age = @Age, Id = @Id",new{Age=(int?)null,Id=guid});dog.Count().IsEqualTo(1);dog.First().Age.IsNull();dog.First().Id.IsEqualTo(guid);

Execute a query and map it to a list of dynamic objects

publicstaticIEnumerable<dynamic>Query(thisIDbConnectioncnn,stringsql,objectparam=null,SqlTransactiontransaction=null,boolbuffered=true)

This method will execute SQL and return a dynamic list.

Example usage:

varrows=connection.Query("select 1 A, 2 B union all select 3, 4");((int)rows[0].A).IsEqualTo(1);((int)rows[0].B).IsEqualTo(2);((int)rows[1].A).IsEqualTo(3);((int)rows[1].B).IsEqualTo(4);

Execute a Command that returns no results

publicstaticintExecute(thisIDbConnectioncnn,stringsql,objectparam=null,SqlTransactiontransaction=null)

Example usage:

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}).IsEqualTo(2);

Execute a Command multiple times

The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)

Example usage:

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}}).IsEqualTo(3);// 3 rows inserted: "1,1", "2,2" and "3,3"

This works for any parameter that implements IEnumerable for some T.

Performance

A key feature of Dapper is performance. The following metrics show how long it takes to execute 500 SELECT statements against a DB and map the data returned to objects.

The performance tests are broken in to 3 lists:

  • POCO serialization for frameworks that support pulling static typed objects from the DB. Using raw SQL.
  • Dynamic serialization for frameworks that support returning dynamic lists of objects.
  • Typical framework usage. Often typical framework usage differs from the optimal usage performance wise. Often it will not involve writing SQL.

Performance of SELECT mapping over 500 iterations - POCO serialization

MethodDurationRemarks
Hand coded (using a SqlDataReader)47ms
Dapper ExecuteMapperQuery49ms
ServiceStack.OrmLite (QueryById)50ms
PetaPoco52msCan be faster
BLToolkit80ms
SubSonic CodingHorror107ms
NHibernate SQL104ms
Linq 2 SQL ExecuteQuery181ms
Entity framework ExecuteStoreQuery631ms

Performance of SELECT mapping over 500 iterations - dynamic serialization

MethodDurationRemarks
Dapper ExecuteMapperQuery (dynamic)48ms
Massive52ms
Simple.Data95ms

Performance of SELECT mapping over 500 iterations - typical usage

MethodDurationRemarks
Linq 2 SQL CompiledQuery81msNot super typical involves complex code
NHibernate HQL118ms
Linq 2 SQL559ms
Entity framework859ms
SubSonic ActiveRecord.SingleOrDefault3619ms

Performance benchmarks are available here.

Feel free to submit patches that include other ORMs - when running benchmarks, be sure to compile in Release and not attach a debugger (ctrl F5).

Alternatively, you might prefer Frans Bouma's RawDataAccessBencher test suite or OrmBenchmark.

Parameterized queries

Parameters are passed in as anonymous classes. This allow you to name your parameters easily and gives you the ability to simply cut-and-paste SQL snippets and run them in Query analyzer.

new{A=1,B="b"}// A will be mapped to the param @A, B to the param @B 

List Support

Dapper allows you to pass in IEnumerable and will automatically parameterize your query.

For example:

connection.Query<int>("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids",new{Ids=newint[]{1,2,3}});

Will be translated to:

select*from(select1asId union allselect2union all select3)as X where Id in(@Ids1,@Ids2,@Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3

Buffered vs Unbuffered readers

Dapper's default behavior is to execute your sql and buffer the entire reader on return. This is ideal in most cases as it minimizes shared locks in the db and cuts down on db network time.

However when executing huge queries you may need to minimize memory footprint and only load objects as needed. To do so pass, buffered: false into the Query method.

Multi Mapping

Dapper allows you to map a single row to multiple objects. This is a key feature if you want to avoid extraneous querying and eager load associations.

Example:

Consider 2 classes: Post and User

classPost{publicintId{get;set;}publicstringTitle{get;set;}publicstringContent{get;set;}publicUserOwner{get;set;}}classUser{publicintId{get;set;}publicstringName{get;set;}}

Now let us say that we want to map a query that joins both the posts and the users table. Until now if we needed to combine the result of 2 queries, we'd need a new object to express it but it makes more sense in this case to put the User object inside the Post object.

This is the user case for multi mapping. You tell dapper that the query returns a Post and a User object and then give it a function describing what you want to do with each of the rows containing both a Post and a User object. In our case, we want to take the user object and put it inside the post object. So we write the function:

(post,user)=>{post.Owner=user;returnpost;}

The 3 type arguments to the Query method specify what objects dapper should use to deserialize the row and what is going to be returned. We're going to interpret both rows as a combination of Post and User and we're returning back a Post object. Hence the type declaration becomes

<Post,User,Post>

Everything put together, looks like this:

varsql=@"select * from #Posts p left join #Users u on u.Id = p.OwnerId Order by p.Id";vardata=connection.Query<Post,User,Post>(sql,(post,user)=>{post.Owner=user;returnpost;});varpost=data.First();post.Content.IsEqualTo("Sams Post1");post.Id.IsEqualTo(1);post.Owner.Name.IsEqualTo("Sam");post.Owner.Id.IsEqualTo(99);

Dapper is able to split the returned row by making an assumption that your Id columns are named Id or id. If your primary key is different or you would like to split the row at a point other than Id, use the optional splitOn parameter.

Multiple Results

Dapper allows you to process multiple result grids in a single query.

Example:

varsql=@"select * from Customers where CustomerId = @idselect * from Orders where CustomerId = @idselect * from Returns where CustomerId = @id";using(varmulti=connection.QueryMultiple(sql,new{id=selectedId})){varcustomer=multi.Read<Customer>().Single();varorders=multi.Read<Order>().ToList();varreturns=multi.Read<Return>().ToList();
...}

Stored Procedures

Dapper fully supports stored procs:

varuser=cnn.Query<User>("spGetUser",new{Id=1},commandType:CommandType.StoredProcedure).SingleOrDefault();

If you want something more fancy, you can do:

varp=newDynamicParameters();p.Add("@a",11);p.Add("@b",dbType:DbType.Int32,direction:ParameterDirection.Output);p.Add("@c",dbType:DbType.Int32,direction:ParameterDirection.ReturnValue);cnn.Execute("spMagicProc",p,commandType:CommandType.StoredProcedure);intb=p.Get<int>("@b");intc=p.Get<int>("@c");

Ansi Strings and varchar

Dapper supports varchar params, if you are executing a where clause on a varchar column using a param be sure to pass it in this way:

Query<Thing>("select * from Thing where Name = @Name",new{Name=newDbString{Value="abcde",IsFixedLength=true,Length=10,IsAnsi=true});

On SQL Server it is crucial to use the unicode when querying unicode and ansi when querying non unicode.

Type Switching Per Row

Usually you'll want to treat all rows from a given table as the same data type. However, there are some circumstances where it's useful to be able to parse different rows as different data types. This is where IDataReader.GetRowParser comes in handy.

Imagine you have a database table named "Shapes" with the columns: Id, Type, and Data, and you want to parse its rows into Circle, Square, or Triangle objects based on the value of the Type column.

varshapes=newList<IShape>();using(varreader=connection.ExecuteReader("select * from Shapes")){// Generate a row parser for each type you expect.// The generic type <IShape> is what the parser will return.// The argument (typeof(*)) is the concrete type to parse.varcircleParser=reader.GetRowParser<IShape>(typeof(Circle));varsquareParser=reader.GetRowParser<IShape>(typeof(Square));vartriangleParser=reader.GetRowParser<IShape>(typeof(Triangle));vartypeColumnIndex=reader.GetOrdinal("Type");while(reader.Read()){IShapeshape;vartype=(ShapeType)reader.GetInt32(typeColumnIndex);switch(type){caseShapeType.Circle:shape=circleParser(reader);break;caseShapeType.Square:shape=squareParser(reader);break;caseShapeType.Triangle:shape=triangleParser(reader);break;default:thrownewNotImplementedException();}shapes.Add(shape);}}

Limitations and caveats

Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. The objects it stores are never flushed. If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues. We may convert the dictionaries to an LRU Cache.

Dapper's simplicity means that many feature that ORMs ship with are stripped out. It worries about the 95% scenario, and gives you the tools you need most of the time. It doesn't attempt to solve every problem.

Will Dapper work with my DB provider?

Dapper has no DB specific implementation details, it works across all .NET ADO providers including SQLite, SQL CE, Firebird, Oracle, MySQL, PostgreSQL and SQL Server.

Do you have a comprehensive list of examples?

Dapper has a comprehensive test suite in the test project

Who is using this?

Dapper is in production use at:

Stack Overflow, helpdesk

(if you would like to be listed here let me know)

About

Dapper - a simple object mapper for .Net

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages