Dapper.Rainbow VS Dapper.Contrib [英] Dapper.Rainbow VS Dapper.Contrib

查看:530
本文介绍了Dapper.Rainbow VS Dapper.Contrib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释 Dapper.Rainbow Dapper.Contrib 之间的区别吗?

Can someone please explain the difference between Dapper.Rainbow vs. Dapper.Contrib?

我的意思是什么时候使用Dapper.Contrib的SqlMapperExtensions.cs,什么时候应该使用Dapper.Rainbow?

I mean when do you use SqlMapperExtensions.cs of Dapper.Contrib and when should you use Dapper.Rainbow?

推荐答案

我已经使用Dapper一段时间了,想知道彩虹项目都是关于我自己的.经过一番代码审查,以下是我对它们的用法的看法:

I’ve been using Dapper for a while now and have wondered what the Contrib and Rainbow projects were all about myself. After a bit of code review, here are my thoughts on their uses:

Contrib在IDbConnection接口上提供了一组扩展方法,用于基本的CRUD操作:

Contrib provides a set of extension methods on the IDbConnection interface for basic CRUD operations:

  • 获取
  • 插入
  • 更新
  • 删除

Contrib的关键组件是它为您的实体提供跟踪,以识别是否进行了更改.

The key component of Contrib is that it provides tracking for your entities to identify if changes have been made.

例如,使用带有接口作为类型约束的Get方法将返回带有内部字典的动态生成的代理类,以跟踪已更改的属性.

For example, using the Get method with an interface as the type constraint will return a dynamically generated proxy class with an internal dictionary to track what properties have changed.

然后可以使用Update方法,该方法将生成仅更新那些已更改的属性所需的SQL.

You can then use the Update method which will generate the SQL needed to only update those properties that have changed.

主要警告:要获得Contrib的跟踪优势,必须使用接口作为类型约束来允许生成代理类.

Major Caveat: to get the tracking goodness of Contrib, you must use an Interface as your type constraint to allow the proxy class to be generated.

彩虹是一个抽象类,您可以将其用作Dapper类的基类以提供基本的CRUD操作:

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

  • 获取
  • 插入
  • 更新
  • 删除

以及一些常用方法,例如First(获取表中的第一条记录)和All(获取表中的所有结果记录).

As well as some commonly used methods such as First (gets the first record in a table) and All (gets all results records in a table).

出于所有目的和目的,Rainbow基本上是您最常用的数据库交互的包装,并且将基于属性名称和类型约束构建无聊的SQL.

For all intents and purposes, Rainbow is basically a wrapper for your most commonly used database interactions and will build up the boring SQL based on property names and type constraints.

例如,通过Get操作,Rainbow将建立一个普通的SQL查询并返回所有列,然后将这些值映射回用作约束的类型.

For example, with a Get operation, Rainbow will build up a vanilla SQL query and return all columns and then map those values back to the type used as the constraint.

类似地,insert/update方法将根据类型约束的属性名称动态构建插入/更新所需的SQL.

Similarly, the insert/update methods will dynamically build up the SQL needed for an insert/update based on the type constraint's property names.

主要警告:Rainbow希望您的所有表都具有一个名为"Id"的标识列.

Major Caveat: Rainbow expects all your tables to have an identity column named "Id".

Contrib和Rainbow的主要区别是(IMO),一个跟踪您的实体的更改,另一个跟踪您的实体的更改:

The major difference between Contrib and Rainbow is (IMO), one tracks changes to your entities, the other doesn’t:

  • 当您希望能够跟踪实体中的更改时,请使用Contrib.
  • 当您想按照标准ADO.NET方法使用更多内容时,请使用Rainbow.

在旁注:我希望早些时候研究Rainbow,因为我建立了与Dapper一起使用的非常相似的基类.

摘自文章并引用@anthonyv引用:

From the article and quote @anthonyv quoted: That annoying INSERT problem, getting data into the DB

现在您还可以选择其他2个API(除了Rainbow )(适用于CRUD) Dapper.Contrib Dapper扩展. 我认为不可能一刀切.根据您的问题和 偏好设置中可能有一个最适合您的API.我试过了 介绍一些选项.没有祝福的最佳方法"来解决 世界上的每一个问题.

There are now 2 other APIs you can choose from as well (besides Rainbow) (for CRUD) Dapper.Contrib and Dapper Extensions. I do not think that one-size-fits-all. Depending on your problem and preferences there may be an API that works best for you. I tried to present some of the options. There is no blessed "best way" to solve every problem in the world.

我怀疑Sam在上述报价和相关博客文章中试图传达的内容是:您的方案可能需要大量自定义映射(使用Vanilla Dapper),或者可能需要跟踪实体更改(使用Contrib),或者您可能有常见的使用场景(使用Rainbow),或者可能希望将它们全部组合使用.甚至不使用Dapper. YMMV.

I suspect what Sam was trying to convey in the above quote and the related blog post was: Your scenario may require a lot of custom mapping (use vanilla Dapper), or it may need to track entity changes (use Contrib), or you may have common usage scenarios (use Rainbow) or you may want to use a combination of them all. Or not even use Dapper. YMMV.

这篇关于Dapper.Rainbow VS Dapper.Contrib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆