使用 Castle Active Record 与 Straight NHibernate 的优缺点是什么?

发布于 2024-07-09 23:56:22 字数 148 浏览 3 评论 0原文

假设编写 nhibernate 映射文件不是一个大问题......或者用属性污染你的域对象也不是一个大问题......

有什么优点和缺点?

是否存在任何基本技术问题? 什么因素会影响人们的选择?

不太确定所有的权衡是什么。

Assuming that writing nhibernate mapping files is not a big issue....or polluting your domain objects with attributes is not a big issue either....

what are the pros and cons?

is there any fundamental technical issues? What tends to influence peoples choice?

not quite sure what all the tradeoffs are.

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

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

发布评论

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

评论(3

凉世弥音 2024-07-16 23:56:22

AR 最大的优点是它为您提供了一个现成的存储库并为您负责会话管理。 ActiveRecordBaseActiveRecordMediator 中的任何一个都是一份礼物,您最终会在 NHibernate 下自行组装。 避免 XML 映射是另一个优点。 AR 映射属性使用简单,但足够灵活,甚至可以映射相当“传统”的数据库。

AR 最大的缺点是它会积极鼓励你错误地思考 NHibernate。 也就是说,因为默认的会话管理是每次调用一个会话,所以您习惯了这样的想法:持久化对象是断开连接的,并且在发生更改时必须Save()d。 这不是 NHibernate 应该如何工作的 - 通常你会有每个工作单元或请求或线程的会话,并且对象将在会话的生命周期中保持连接,因此更改会自动保留。 如果你开始使用 AR,然后发现你需要切换到每个请求会话来进行延迟加载工作 - 这在文档中没有很好地解释 - 当你不期望的对象时,你会得到一个令人讨厌的惊喜当会话刷新时保存。

请记住,Castle 团队将 AR 编写为 Castle Monorail 的补充产品,Castle Monorail 是一个类似于 Rails 的 .NET 框架。 它的设计就考虑到了这种用途。 它不能很好地适应分层、解耦的设计。

按原样使用它,但不要将其视为 NHibernate 的快捷方式。 如果您想使用 NH 但避免映射文件,请使用 NHibernate 属性或更好的 Fluent NHibernate。

The biggest pro of AR is that it gives you a ready-made repository and takes care of session management for you. Either of ActiveRecordBase<T> and ActiveRecordMediator<T> are a gift that you would have ended up assembling yourself under NHibernate. Avoiding the XML mapping is another plus. The AR mapping attributes are simple to use, yet flexible enough to map even fairly 'legacy' databases.

The biggest con of AR is that it actively encourages you to think incorrectly about NHibernate. That is, because the default session management is session-per-call, you get used to the idea that persisted objects are disconnected and have to be Save()d when changes happen. This is not how NHibernate is supposed to work - normally you'd have session-per-unit-of-work or request or thread, and objects would remain connected for the lifecycle of the session, so changes get persisted automatically. If you start off using AR and then figure out you need to switch to session-per-request to make lazy loading work - which is not well explained in the docs - you'll get a nasty surprise when an object you weren't expecting to get saved does when the session flushes.

Bear in mind that the Castle team wrote AR as a complementary product for Castle Monorail, which is a Rails-like framework for .NET. It was designed with this sort of use in mind. It doesn't adapt well to a more layered, decoupled design.

Use it for what it is, but don't think about it as a shortcut to NHibernate. If you want to use NH but avoid mapping files, use NHibernate Attributes or better, Fluent NHibernate.

数理化全能战士 2024-07-16 23:56:22

我发现 ActiveRecord 是一个很好的工具包,非常适合我使用它的中小型项目。 与 Rails 一样,它为您做出许多重要的决定,这可以让您将注意力集中在问题的实质上。

在我看来,优点和缺点是:

优点

  • 让您专注于手头的问题,因为许多决定都是为您做出的。
  • 包括成熟、非常有用的基础设施类(存储库、验证等)
  • 编写 AR 属性比编写 XML 或 NHibernate.Mapping.Attributes 恕我直言更快。
  • 良好的文档和社区支持
  • 使用 NHibernate 的其他功能相当容易。
  • 安全的开始。 你有一个退出条款。 如果您使用 AR 遇到困难,您可以慢慢回到定制的 NHibernate 解决方案。
  • 非常适合领域优先开发(生成数据库)。
  • 您可能还想了解 ActiveRecord 模式

缺点

  • 你不能假装NHibernate不存在——你仍然需要学习它。
  • 如果您已经有一个旧数据库可供使用,那么生产力可能不会那么高。
  • 不透明的持久性。
  • 内置映射很全面,但对于某些项目,您可能需要在某些地方恢复到 NHibernate 映射。 我没有遇到过这个问题,只是一个想法。

总的来说,我真的很喜欢 ActiveRecord,它一直可以节省时间,主要是因为我似乎很高兴接受库中的决策和工具,然后花更多的时间专注于手头的问题。

我会在几个项目上尝试一下,看看你的想法。

I found ActiveRecord to be a good piece of kit, and very suitable for the small/medium projects I've used it for. Like Rails, it makes many important decisions for you, which has the effect of keeping you focused you on the meat of the problem.

In my opinion pro's and cons are:

Pros

  • Lets you focus on problem in hand, because many decisions are made for you.
  • Includes mature, very usable infrastructure classes (Repository, Validations etc)
  • Writing AR attributes are faster than writing XML or NHibernate.Mapping.Attributes IMHO.
  • Good documentation and community support
  • It's fairly easy to use other NHibernate features with it.
  • A safe start. You have a get-out clause. You can slowly back into a bespoke NHibernate solution if you hit walls with AR.
  • Great for domain-first development (generating the db).
  • You might also want to look up the benefits and drawbacks of the ActiveRecord pattern

Cons

  • You can't pretend NHibernate isn't there - you still need to learn it.
  • Might not be so productive if you already have a legacy database to work with.
  • Not transparent persistence.
  • In-built mappings are comprehensive, but for some projects you might need to revert to NHibernate mappings in places. I haven't had this problem, but just a thought.

In general, I really like ActiveRecord and it's always been a time saver, mainly because I seem to happily accept the decisions and tools baked into the library, and subsequently spend more time focusing on the problem in hand.

I'd give it a try on a few projects and see what you think.

分開簡單 2024-07-16 23:56:22

当我开始使用 NHibernate 时,直到我编写了映射文件并制作了我的映射文件后,我才了解 Castle ActiveRecord类。 那时,我无法明显看出 Castle Activerecord 能给我带来什么,所以我没有使用它。

我第二次使用 NHibernate 时,我只是使用 myGeneration 来只需让它查看我的数据库即可制作映射文件和类。 这本身就节省了很多时间,并且让我(再一次)不用担心 Castle Active Record。

实际上,您的大部分时间将花在进行自定义查询上,而 Castle Active Record 不一定会对此有所帮助 - 如果您将 myGeneration 与 NHibernate 一起使用,您将绕过大部分工作无论如何都需要做。

编辑:我不想让自己看起来像 myGeneration 或 NHibernate 的啦啦队长。 我只是使用能让我快速轻松地完成工作的工具。 我花在编写数据访问代码上的时间越少越好。 这并不意味着我做不到——但是每次编写新应用程序时重新发明轮子没有什么意义。 在需要的地方编写 SQL 查询和存储过程,而不是在其他地方编写。 如果您正在进行 CRUD 操作,ORM 是最佳选择。

编辑 #2:Castle Active Record 可能会带来比我想象的更多的东西 - 我不知道太多其他 比他们网站上的内容,但如果它确实带来了更多内容,那么它将帮助潜在的采用者能够轻松地在他们的网站上看到这些内容。

When I started using NHibernate, I didn't learn about Castle ActiveRecord until I had written my Mapping files and made my classes. At that point, I couldn't visibly discern what Castle Activerecord would give me, so I didn't use it.

The second time I used NHibernate, I simply used myGeneration to make the mapping files and the classes just by having it look at my database. That saved a lot of time by itself, and allowed me to (once again) not worry about Castle Active Record.

In reality, most of your time is going to be spent making the custom queries, and Castle Active Record won't necessarily help with that -- if you were to use myGeneration with NHibernate, you'd bypass most of the work you'd need to do anyway.

Edit: I don't want to seem like a cheerleader for either myGeneration or NHibernate. I just use the tool that allows me to get my work done quickly and easily. The less time I have to spend writing Data Access code, the better. It doesn't mean I can't do it -- but there's little sense in re-inventing the wheel each time you write a new application. Write SQL queries and Stored Procedures where needed, and no where else. If you're doing CRUD operations, an ORM is the way to go.

Edit #2: Castle Active Record may bring more to the table than I realize -- I don't know much other than what's on their website, but if it does bring more to the table, then it would help potential adopters to be able to readily see that on their site.

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