Castle ActiveRecord 中的交易 + NHibernate 傻瓜版

发布于 2024-08-18 21:28:04 字数 235 浏览 2 评论 0原文

我想在单个原子事务中执行以下操作:

  1. 通过 ActiveRecord 读取对象
  2. 通过 ActiveRecord 保存新对象 通过
  3. NHibernate 更新另一个表

另外,如果在我完成事务时,我已获取对象的值步骤 1 中的内容已更改,我希望交易失败。

我以前从未在 AR 或 NH 中使用过交易,因此请提供尽可能多的详细信息。交易机制如何协同运作?

I would like to do the following, all in a single atomic transaction:

  1. Read an object through ActiveRecord
  2. Save a new object through ActiveRecord
  3. Update another table via NHibernate

Also, if by the time I finish the transaction, the value of the object I've read in step 1 has changed, I would like the transaction to fail.

I've never used transactions before in either AR or NH, so please supply as much details as you can. How do the transaction mechanisms play together?

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

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

发布评论

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

评论(2

怼怹恏 2024-08-25 21:28:04

嗯,Castle ActiveRecord 依赖于 NHibernate。确实,Castle ActiveRecord Transaction 类与 NHibernate 上的类不同;但是您应该将 ActiveRecord 视为 NHibernate 中 ActiveRecord 的包装。

所以没必要“一起玩”。因为,在内心深处,它们是相同的。

另一个重要的概念是“会话”。 “会话”是一个工作单元,是一个“窗口”,您可以用它告诉 NHibernate 您想要做什么:查询、更新、插入等。同样,有一个 NHibernate 会话,还有一个 Castle ActiveRecord 会话。同样,后者是第一个的包装。

这很重要,因为当您在 ActiveRecord 中使用会话时(事实上您总是这样做,即使是隐式的),可以访问其中的“隐藏”NHibernate 会话;通常使用传递给 Execute 方法的委托。这就是您可以在应用程序中使用两种代码风格的方式(请参阅 在此处执行回调示例)。

要更好地掌握会话含义,请参阅在这里

最后,我建议您始终使用 ActiveRecord 的样式来声明会话和事务,即使混合两种编码样式也是如此。由于它们还代表 NHibernate 会话和事务,因此您是安全的。为了更好地理解这一点,请阅读此处

PS:是的,我没有写你要求的例子(太懒了),但我认为如果你写它会对你的学习过程更好。相信我,这比你想象的要容易。

Well, Castle ActiveRecord depends on NHibernate. True, a Castle ActiveRecord Transaction class is not the same as the one found on NHibernate; but you should think of ActiveRecord's as a wrapper over the one in NHibernate.

So there is no need of "playing together". Because, deep down, they are the same.

Another important concept is that of "Session". A "session" is a unit of work, a "window" that you use to tell NHibernate what you want to do: queries, updates, inserts etc. Again, there is a NHibernate Session, and a Castle ActiveRecord Session too. Again, the latter is a wrapper over the first one.

This is important because when you're using a session in ActiveRecord (and in fact you always do, even if implicitly), it is possible to access the "hidden" NHibernate session inside it; usually using a delegate passed to the Execute method. And that's the way that you can use both styles of code inside your application (see the Execute Callback example here).

To get a better grasp of session meaning, see here.

Finally, I suggest that you always use ActiveRecord's style to declare sessions and transactions, even when mixing both styles of coding. Since they represent also NHibernate sessions and transactions, you are safe. To better understand that, please read here.

PS: Yep, I didn't write the example you asked for (too lazy for that), but I reckon it will be better for your learning process if you write it. Believe me, it's easier than you think.

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