11

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.

user1003841
  • 121
  • 1
  • 5

3 Answers3

6

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

Marc L.
  • 3,296
  • 1
  • 32
  • 42
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • `Dapper.Contrib` in Nuget ? Using `Dapper.Contrib` and `Dapper.Rainbow` in the same project is good practice ? – Kiquenet Feb 15 '17 at 14:31
5

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

Community
  • 1
  • 1
Johan Danforth
  • 4,469
  • 6
  • 37
  • 36
  • I'm sure I'm missing something dumb & obvious - but I get "user does not contain a definition for IsNull" on line 3. Where does IsNull come from? thx. – niico May 22 '16 at 23:17
  • 1
    The IsNull() method is just a simple test-method which is included in the Dapper-source on GitHub – Johan Danforth Jun 17 '16 at 07:50
3

I think user1003841 was referring to https://github.com/tmsmith/Dapper-Extensions.

The authors are Thad Smith and Page Brooks - so it's not Sam Saffron's work. The project page says "This library is a separate effort from Dapper.Contrib".

Mike Gleason
  • 1,479
  • 14
  • 17
  • "The authors are Thad Smith and Page Brooks - so it's not Sam Saffron's work." made me lol because of who the original answer came from – Terry Apr 04 '12 at 19:39