Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper SqlMapperExtensions / Dapper.Contrib?

Tags:

.net

orm

dapper

There seems to be a DapperExtensions project, but there is also a SqlMapperExtensions class in the Dapper project. Is there overlap? Is one preferred over the other? I can't find any documentation on Dapper.Contrib.

like image 370
user1003841 Avatar asked Nov 22 '11 20:11

user1003841


People also ask

What is dapper contrib?

Contrib - a simple object mapper for . Net.

What is dapper rainbow?

Rainbow. Rainbow is an Abstract class that you can use as a base class for your Dapper classes to provide basic CRUD operations: Get. Insert.


2 Answers

I wrote the first Dapper.Contrib a long time ago after some discussion with Sam. I don't know the details of the Extensions-package and they seem to do the same CRUD-thing more or less but the Contrib-package may be somewhat faster in some scenarios because it has a built in cache for both queries and for interface-based POCOs with an internal "is dirty" tracking. Snipped from the test-code:

        using (var connection = GetOpenConnection())
        {
            connection.Get<User>(3).IsNull();

            var id = connection.Insert(new User {Name = "Adam", Age = 10});

            //get a user with "isdirty" tracking
            var user = connection.Get<IUser>(id);
            user.Name.IsEqualTo("Adam");
            connection.Update(user).IsEqualTo(false);    //returns false if not updated, based on tracking
            user.Name = "Bob";
            connection.Update(user).IsEqualTo(true);    //returns true if updated, based on tracking
            user = connection.Get<IUser>(id);
            user.Name.IsEqualTo("Bob");

            //get a user with no tracking
            var notrackedUser = connection.Get<User>(id);
            notrackedUser.Name.IsEqualTo("Bob");
            connection.Update(notrackedUser).IsEqualTo(true);   //returns true, even though user was not changed
            notrackedUser.Name = "Cecil";
            connection.Update(notrackedUser).IsEqualTo(true);
            connection.Get<User>(id).Name.IsEqualTo("Cecil");

            connection.Query<User>("select * from Users").Count().IsEqualTo(1);
            connection.Delete(user).IsEqualTo(true);
            connection.Query<User>("select * from Users").Count().IsEqualTo(0);

            connection.Update(notrackedUser).IsEqualTo(false);   //returns false, user not found

Contrib does not have the nice looking predicate system which Extensions has. NOTE there is a good thread on Dapper.Contrib here Dapper.Rainbow VS Dapper.Contrib

like image 34
Johan Danforth Avatar answered Sep 27 '22 20:09

Johan Danforth


Dapper.Contrib is the assembly name: https://github.com/StackExchange/Dapper/tree/master/Dapper.Contrib

SqlMapperExtensions is the static class containing the contrib methods within Dapper.Contrib: https://github.com/StackExchange/Dapper/blob/master/Dapper.Contrib/SqlMapperExtensions.cs

The best documentation is the test case class: https://github.com/StackExchange/Dapper/blob/master/Dapper.Tests.Contrib/TestSuite.cs

like image 50
Sam Saffron Avatar answered Sep 27 '22 19:09

Sam Saffron