Dapper 与 ADO.NET 相比如何? [英] How does Dapper compare to ADO.NET?

查看:47
本文介绍了Dapper 与 ADO.NET 相比如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候应该使用 Dapper 而不是 ADO.NET?

When should Dapper be used instead of ADO.NET?

我想了解 Dapper 相对于 ADO.NET 的优缺点.Dapper 的哪些优势会促使其使用?

I would like to understand the pros and cons of Dapper over ADO.NET. What are the advantages of Dapper that would motivate its use?

推荐答案

Dapper 只是一个工具.它的作用是:

Dapper is just a tool. What it does is:

  • 让正确参数化查询变得非常容易
  • 让查询的执行变得非常简单(标量、多行、多网格和无结果)
  • 让结果转化为对象变得非常简单
  • 非常高效和快速

做的是:

  • 为你生成一个类模型
  • 为您生成查询
  • 跟踪对象及其更改,以便您只需调用 SubmitChanges()(或其他)

原始 dapper 库不提供 CRUD 功能,但contrib"附加包确实提供基本 CRUD.

The raw dapper library doesn't provide CRUD features, but the "contrib" additional package does provide basic CRUD.

基本上,它不是一个全权重的 ORM,但如果您只想运行查询而不必一个 ORM 或支付与 ORM 相关的开销,它非常棒.如果您不了解 SQL,那么原始库可能不适合您(不过,contrib"应该没问题),但是很多人不仅知道 SQL,而且还想控制 SQL(而不是让 ORM 对您的意图做出一些尚未优化的解释等).

Basically, it isn't a full-weight ORM, but if you just want to run queries without having to fight an ORM, or pay the overheads associated with an ORM, it is pretty great. If you don't know SQL, the raw library probably isn't for you ("contrib" should be fine, though), but lots of people not only know SQL, but they want to be in control of the SQL (rather than letting the ORM come up with some interpretation of your intent that has not been optimized, etc).

总而言之,原因可能是:

To summarize, reasons might be:

  • 您希望以最小的开销获得出色的原始执行性能
  • 您希望保留对 SQL 的控制权
  • 您不需要也不想要全脂 ORM 的对象跟踪功能

至于vs ADO.NET":

As for "vs ADO.NET":

  • 原始 ADO.NET 涉及 很多 更多代码要编写和许多边缘情况要记住(dapper 在内部处理而您无需担心它们)
  • 但它实际上并没有更快 - dapper 在完成查询所需的操作后会执行大量元编程来存储和重用策略
  • 如果您正在使用原始 ADO.NET 中不可用的特定于提供程序的功能(例如,传递/获取 SqlGeometry 数据),则这些功能不是直接在 dapper 中可用 - 您需要实现一个接口来告诉它如何处理您的场景,但这并不难(请注意,特定的 SqlGeometry 示例由额外的 dapper 库处理)
  • raw ADO.NET involves a lot more code to write and a lot of edge-cases to remember about (that dapper deals with internally without you needing to worry about them)
  • but it isn't actually faster - dapper does a lot of meta-programming to store and re-use strategies once it has done what it needs for your query
  • if you are using provider-specific features that aren't available in raw ADO.NET (for example, passing/fetching SqlGeometry data), those are not directly availalbe in dapper - you'd need to implement an interface to tell it how to handle your scenario, but that isn't hard (note that the specific SqlGeometry example is handled by an additional dapper library)

这篇关于Dapper 与 ADO.NET 相比如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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