NHibernate 会话管理和延迟加载

发布于 2024-07-25 17:35:24 字数 789 浏览 3 评论 0原文

我花了很长时间试图解决 NHibernate 中的会话管理问题。 我假设我的很多麻烦是由于缺乏 IoC 和 AOP 概念的知识; 至少法比奥·莫洛(Fabio Maulo)一直在指导我,我就是这么想的。

无论如何,我的问题是我有一个 win 表单应用程序,它正在进行“get”调用并将结果绑定到网格。 绑定后,用户可以执行某种“写入”操作,这些操作会导致会话在写入后关闭,以尝试使用每次使用会话概念。 然后用户可以滚动网格,这会导致延迟加载开始,现在会话已关闭,我得到一个异常。

我不想让我的视图认识到我的会话,我不想在用户关闭表单时发送 KillAllSessions。 另外,用户可能在任何给定时间打开多个表单,这进一步加剧了与该方法相关的问题。 我本质上希望所有这一切都在“幕后”进行。

所以到目前为止我的想法是拦截延迟加载调用并检查会话是否打开,如果没有重新打开它,获取信息然后重新关闭它。 然而,据我所知,这并不多,这本质上就是延迟加载的工作原理。 它被代理工厂(NHibernate.Bytecode.Castle)拦截,然后使用会话检索数据。 因此,我需要实际拦截该调用,然后在重新打开会话后将其传递给原始预期的拦截。 这就是我的想法。

我的问题本质上首先是这是否是解决此问题的正确方法? 其次,如果是的话,我什至不知道从哪里开始。 我从来没有做过任何方法调用的拦截,我在理论上知道,但在实践中却没有。 我知道有一些库可以做这种事情,比如 Rhino Commons,但我想借此机会学习并成为一名更好的程序员。 我正在尝试理解 AOP 和上下文绑定对象,但目前我还没有理解它。 你们中的一些人可以帮助一个人吗?

I am having a heck of a time trying to figure out my session management woes in NHibernate. I am assuming that a lot of my trouble is due to lack of knowledge of IoC and AOP concepts; at least that is what I am thinking by where Fabio Maulo keeps directing me.

Anyways, my problem is that I have a win forms application that is making "get" calls and binding the result to a grid. After binding the user may perform some kind of "write" action and those result in the session being closed after the write in an attempt to use the session per use concept. Then the user may scroll through the grid which causes the lazy loading to kick off and now the session has been closed and I get an exception.

I do not want to make my view cognizant of my sessions, I don't want to send off a KillAllSessions when the user closes the form. Plus a user may have multiple forms open at any given time further compounding the issues associated with that method. I essentially want all of this to work "behind the scenes".

So my idea thus far is to intercept the lazy loading call and check to see if the session is open and if not re-open it, get the information then re-close it. However, as far as I can tell, which isn't much, this is essentially how the lazy loading works anyways. It is intercepted by the proxy factory (NHibernate.Bytecode.Castle) and then retrieves the data using the session. So I need to actually intercept that call then pass it on to the original intended intercept after re-opening the session. So that is my idea.

My question is essentially first of all is this even the right way to go about this? Second if it is I don't even know where to start. I have never done any intercepting of method calls, I knew of it in theory but not in practice. I know there are libraries out there that do this kind of thing such as Rhino Commons, but I want to take this opportunity to learn and become a better programmer. I am trying to understand AOP and Context Bound Objects but currently I am not grokking it. Could some of you folks please help a guy out?

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

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

发布评论

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

评论(2

时常饿 2024-08-01 17:35:24

我可以想到几个选项:

选项 1:在用户与数据交互时保持原始 ISession 打开,并在用户完成后立即提交所有更改。 这意味着内存中可能有大量未提交的更改,其他用户将看不到挂起的更改。

选项 2:将操作拆分为两个工作单元 (UOW)。 UOW1 仅执行读取操作并负责填充列表。 与 UOW1 关联的 ISession 保持活动状态以允许延迟加载,例如在向下钻取场景中。 UOW2 是一个新的短期 ISession,专为用户编辑而创建。 当编辑提交时,原始对象将从 UOW1 中逐出,并且 UOW1 从数据库中获取新副本。

选项 3:每次编辑提交后重新创建列表。 这是最简单的解决方案,可能适合小型数据集。

I can think of a few options:

Option 1: Keep the original ISession open while the user interacts with the data and commit all the changes at once when the user is done. This means that you may have a large number of uncommitted changes in memory and other users will not see pending changes.

Option 2: Split the operations into two units of work (UOW). UOW1 only does reads and is responsible for populating the list. The ISession associated with UOW1 remains active to allow for lazy loading, such as in a drill-down scenario. UOW2 is a new short-lived ISession that is created for user edits. When an edit commits, the original object is evicted from UOW1 and UOW1 fetches a new copy from the database.

Option 3: Recreate the list after every edit commits. This is the easiest solution and might be suitable for small data sets.

生来就爱笑 2024-08-01 17:35:24

我正在开发一个类似的应用程序。 我正在使用一个我保持打开状态的会话。

每当我写入数据库时​​,我都会使用开始/提交事务,它不会关闭底层会话。 数据库连接仅在事务处理过程中由 NHibernate 打开。

当用户正在积极使用表单时,您是否需要关闭会话?

您能否提供有关您用于管理会话、存储库模式等的更多详细信息?

I am working on a similar application. I am using one session that I keep open.

Whenever I write to the database I use begin/commit transaction which does not close the underlying session. the database connection is only open by NHibernate while the transaction is in progress.

Is there a reason you need to close the session while the user is actively using the form?

Can you provide more details on what you are using to manage your session, repository pattern, ... etc?

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