放弃 ActiveRecord 和 NHibernate——如何重新架构? [英] Ditching ActiveRecord and NHibernate -- how to rearchitect?

查看:21
本文介绍了放弃 ActiveRecord 和 NHibernate——如何重新架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MVC3 NHibernate/ActiveRecord 项目.项目进展顺利,我的模型对象(主要是三个或四个类的巨大层次结构)得到了一些使用.

I have an MVC3 NHibernate/ActiveRecord project. The project is going okay, and I'm getting a bit of use out of my model objects (mostly one giant hierarchy of three or four classes).

我的应用程序是基于分析的;我存储分层数据,然后将其切片,以图表形式显示等,因此实际关系并没有那么复杂.

My application is analytics based; I store hierarchial data, and later slice it up, display it in graphs, etc. so the actual relationship is not that complicated.

到目前为止,我还没有从 ORM 中获益多少;它使查询变得容易 (ActiveRecord),但我经常需要比完整对象更少的信息,而且我需要通过复杂的、多次选择和对集合的迭代来编写硬"查询——原始 SQL 会更快、更清晰.

So far, I haven't benefited much from ORM; it makes querying easy (ActiveRecord), but I frequently need less information than full objects, and I need to write "hard" queries through complex and multiple selects and iterations over collections -- raw SQL would be much faster and cleaner.

所以我正在考虑在这种情况下放弃 ORM,并回到原始 SQL.但我不确定如何重新设计我的解决方案.我应该如何处理数据库层?

So I'm thinking about ditching ORM in this case, and going back to raw SQL. But I'm not sure how to rearchitect my solution. How should I handle the database tier?

  • 我还应该为每个模型创建一个类,使用静态方法来查询对象吗?或者我应该有一个代表数据库的类?
  • 我是否应该在 ActiveRecord 下编写自己的层(或我自己的类似 ActiveRecord 的实现)以保持现有代码或多或少的健全?
  • 我是否应该将 ORM 方法(如保存/删除)合并到我的模型类中?
  • 我是否应该更改我的表结构(每个类一个表,包含所有字段)?

任何建议将不胜感激.我正在尝试找出适合的最佳架构和设计.

Any advice would be appreciated. I'm trying to figure out the best architecture and design to go with.

推荐答案

许多人,包括我自己,认为 ActiveRecord 模式是一种反模式,主要是因为它破坏了 SRP 并且不允许 POCO 对象(将您的域与一个特定的 ORM).

Many, including myself, think the ActiveRecord pattern is an anti-pattern mainly because it breaks the SRP and doesn't allow POCO objects (tightly coupling your domain to a particular ORM).

这么说,你不能在简单的 CRUD 东西上打败 ORM,所以我会为那种工作保留某种 ORM.只需重新构建您的应用程序,以在另一个项目中使用 POCO 对象和某种类型或存储库模式以及您的 ORM 实现细节.

In saying that, you can't beat an ORM for simple CRUD stuff, so I would keep some kind of ORM around for that kind of work. Just re-architect your application to use POCO objects and some kind or repository pattern with your ORM implementation specifics in another project.

至于您的硬"查询,我会考虑使用微小的 ORM(例如 DapperPetaPocoMassive),使用您自己的原始 sql 查询对象.

As for your "hard" queries, I would consider creating one class per view using a tiny ORM (like Dapper, PetaPoco, or Massive), to query the objects with your own raw sql.

这篇关于放弃 ActiveRecord 和 NHibernate——如何重新架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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