如何使用 ActiveRecord 在桌面应用程序中为不同窗口创建不同的会话?
我正在使用 Castle ActiveRecord 构建一个桌面应用程序,并且希望能够为每个窗口窗体执行相当于 1 个 nHibernate 会话的操作。关于如何使用 Active Record 执行此操作有什么想法吗?
具体来说,我有一个主窗口,允许您浏览数据(只读),然后您可以打开单独的表单来编辑数据。
每次打开表单时,我想创建一个新会话,获取要编辑的数据副本(以便可以更改它而不影响主窗口中的数据)。然后我希望能够通过数据绑定来操作该副本。最后,提交更改(如果用户选择保存)或回滚更改(如果用户选择取消)。
有什么想法吗?
I'm building a desktop application with Castle ActiveRecord and want to be able to do the equivalent of 1 nHibernate session per window form. Any ideas on how can I do this with Active Record?
Specifically, I have a main window that allows you to browse the data (read-only) and then you can open separate forms to edit the data.
Each time a form is opened, I want to create a new session, get a copy of the data to edit (so it can be changed without yet impacting the data in the main window). I then want to be able to manipulate that copy via data binding. And finally, either commit the changes (if the user chooses to Save) or roll them back (if the user chooses to cancel).
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不需要延迟加载,请不要使用 SessionScope。您可以简单地进行数据绑定并在保存时调用entity.Save()。由于您的对象是在没有 SessionScope 的情况下分离的,因此您无需在取消表单时执行任何操作。
如果您需要延迟加载,那么这还不适用于普通的 ActiveRecord。计划允许使用 CpBT(在您的情况下为每个表单实例进行对话)的 ConversationalScope,但尚未提供。
一种可能性是尝试 Rhino Commons,它具有 ActiveRecord 的工作单元实现。这可以满足你对当前 AR 的需求,但我已经有大约 2 年没有使用它了。
编辑:
现在可以从 trunk 获得基本的 CpBT 实现。使用 nant 进行检查和构建。请参阅此链接了解如何使用它:
https://svn. castleproject.org/svn/castle/ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/ConversationScenarioTest.cs
它尚未准备好投入生产,但我希望得到有关它的反馈。
If you don't need lazy loading, don't use a SessionScope. You can simply databind and call entity.Save() on Save. Since your objects are detached without a SessionScope, you don't have to do anything on cancellation of the form.
If you need lazy loading, then this won't work with plain ActiveRecord yet. A ConversationalScope that allows using CpBT (in your case Conversation per Form Instance) is planned, but not available yet.
One possibility is to try Rhino Commons which has a unit-of-work-implementation for ActiveRecord. This could allow what you want with current AR, but I didn't use it for about 2 years.
Edit:
A basic CpBT implementation is now available from trunk. Check-out and build with nant. See this link for how to use it:
https://svn.castleproject.org/svn/castle/ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/ConversationScenarioTest.cs
It is not production ready yet, but I'd love feedback on it.