Dapper SqlMapperExtensions / Dapper.Contrib?

孤街浪徒 提交于 2020-06-24 08:40:26

问题


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.


回答1:


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




回答2:


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




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



来源:https://stackoverflow.com/questions/8233314/dapper-sqlmapperextensions-dapper-contrib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!