user1003841
user1003841

Reputation: 121

Dapper SqlMapperExtensions / Dapper.Contrib?

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.

Upvotes: 11

Views: 9326

Answers (3)

Sam Saffron
Sam Saffron

Reputation: 131112

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

Upvotes: 6

Johan Danforth
Johan Danforth

Reputation: 4579

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

Upvotes: 5

Mike Gleason
Mike Gleason

Reputation: 1469

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".

Upvotes: 3

Related Questions