简体   繁体   English

应用程序从EF4迁移到Dapper.net

[英]Application Migration to Dapper.net from EF4

I am working on a large WPF application with Entity framework4 now client is asking to migrate application from EF4 to Dapper.net. 我正在使用Entity Framework4开发大型WPF应用程序,现在客户端要求将应用程序从EF4迁移到Dapper.net。 I am not sure about feasibility & business gain of this migration task as following features of EF4 are missing in Dapper: 我不确定此迁移任务的可行性和业务收益,因为Dapper中缺少EF4的以下功能:

  • Change tracking 变更追踪
  • Lazy-loading 延迟加载
  • Eager fetching 渴望获取
  • Cascades 瀑布
  • Identity map 身份图
  • Unit of work tracking 工作单位跟踪

please suggest me on this or if other better micro ORM exists for WPF applications. 请就此提出建议,或者是否为WPF应用程序提供其他更好的微型ORM。

EF4 and dapper are different tools with different goals and different features. EF4和dapper是具有不同目标和功能的不同工具。 Indeed, it is pretty common to see dapper and tools like EF working side by side (sometimes even against the same object-model) - particularly when you aren't using all the things you've listed in the bullets; 确实,看到dapper和EF之类的工具并排工作(有时甚至针对相同的对象模型)是很普遍的-尤其是当您没有使用项目符号中列出的所有功能时; for example, a lot of "display" code (showing pages to the user, etc) tends to not use those things. 例如,许多“显示”代码(向用户显示页面等)往往不使用那些东西。 For read-only code, it can sometimes be embarrassingly simple to switch in a dapper implementation - just write some SQL and away you go. 对于只读代码,切换到dapper实现有时可能很尴尬地简单-只需编写一些SQL即可。

However! 然而! When it comes to code that does use the features you've mentioned, this is a much more fundamental change; 对于确实使用您提到的功能的代码,这是一个更为根本的改变; it isn't really "migrate" so much as "re-implement". 它并不是真正的“迁移”,而是“重新实现”。 Without things like change tracking, your own code would need to be a bit clearer about what operations each process is performing. 如果没有变更跟踪之类的东西,您自己的代码将需要更加清楚每个进程正在执行哪些操作。 This is actually a good thing, IMO, but is not trivial to retrofit into an existing codebase. 对IMO而言,这实际上是一件好事,但将其改造成现有的代码库并非易事。 If starting from scratch (or: new tables etc in an existing model), it is generally very easy. 如果从头开始 (或:现有模型中的新表等),则通常非常容易。

Personally, the approach I would take here is first to look at your key data fetch operations (especially high frequency, complex query, or large volume) - and consider whether they could be replaced with dapper-based read operations; 就我个人而言,我将在这里采用的方法是首先查看关键数据获取操作(尤其是高频,复杂查询或大容量)-并考虑是否可以将它们替换为基于dapper的读取操作; especially if read-only. 尤其是只读的。 You can probably get some huge wins with minimal work. 您只需付出最少的工作就可以赢得巨大的胜利。 Profiling would be important, and tools like mini-profiler might help. 分析将很重要,而像mini-profiler这样的工具可能会有所帮助。 I would not personally start ripping out an existing, working data layer for the rest of it - that seems like inventing work for the sake of it; 我个人不会开始删除现有的,可用的数据层,就好像为了它而发明工作一样。 and it will be complicated (swapping out an ORM is not a trivial thing). 并且会很复杂(交换一个ORM 并不是一件小事)。

I would , however, put some serious thought into updating to a more recent version of EF ;p 但是,我认真考虑一下,以更新到最新版本的EF; p

(oh, and if it matters: I'm the primary author and maintainer of dapper) (哦,如果重要的话:我是dapper的主要作者和维护者)

In my opinion, the crucial question is why your client want you to migrate this project to another framework. 在我看来,关键的问题是为什么客户希望您将此项目迁移到另一个框架。

If she wants you to perform this migration because of poor performance of some functionalities or modules then you could take Marc's approach and migrate only critical parts of your system. 如果她由于某些功能或模块的性能不佳而希望您执行此迁移,则可以采用Marc的方法,仅迁移系统的关键部分。

In some of my company's projects we use Dapper side by side with full-blown NHibernate stuff and it really works. 在我公司的某些项目中,我们将Dapper与成熟的NHibernate并排使用,这确实有效。 Especially in reporting and batch processing when you probably want to call stored procedures instead of simple CRUD operations. 特别是在报告和批处理中,当您可能想调用存储过程而不是简单的CRUD操作时。

If you decide to follow this path, I'd suggest you to consider using "Unit of work" pattern to enable EF and Dapper to work on same database connection (I've bloged about this matter some time ago). 如果您决定遵循此方法,建议您考虑使用“工作单元”模式来使EF和Dapper能够在同一数据库连接上工作(我在此之前已经写过博客 )。

Of course you have to keep in your mind technical differences like that Dapper (in opposite to "full-blown" ORMs like EF or NH) does not have concept of session and "dirty" objects modified by EF should be read by Dapper only after flushing changes to DB. 当然,您必须牢记技术差异,例如Dapper(与EF或NH等“成熟的” ORM相反)没有会话的概念,EF修改的“脏”对象仅应在Dapper之后读取。将更改刷新到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM