Dapper SqlMapperExtensions / Dapper.Contrib?

发布于 2024-12-17 09:57:55 字数 114 浏览 0 评论 0原文

好像有一个DapperExtensions项目,但是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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唐婉 2024-12-24 09:57:55

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

握住你手 2024-12-24 09:57:55

很久以前,在与 Sam 讨论后,我编写了第一个 Dapper.Contrib。我不知道扩展包的详细信息,它们似乎或多或少做了相同的 CRUD 操作,但在某些情况下,Contrib 包可能会更快一些,因为它有一个用于查询和接口的内置缓存基于 POCO 的内部“脏”跟踪。从测试代码中摘录:

        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 没有 Extensions 拥有的漂亮的谓词系统。 注意这里有一个关于 Dapper.Contrib 的好帖子 Dapper.Rainbow VS Dapper.Contrib

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

一身软味 2024-12-24 09:57:55

我认为 user1003841 指的是 https://github.com/tmsmith/Dapper-Extensions

作者是 Thad Smith 和 Page Brooks - 所以这不是 Sam Saffron 的作品。项目页面显示“这个库是 Dapper.Contrib 的独立项目”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文