WinForms 的 NHibernate 设计

发布于 2024-08-19 19:03:17 字数 136 浏览 3 评论 0原文

我应该如何拥有 NHibernate Session Factory 和 WinForms 应用程序的会话?

SessionFactory 应该是单例吗?或者我可以每次都创建它吗?会话和交易怎么样?

任何帮助将不胜感激。谢谢

How should I have the NHibernate Session Factory and the sessions for a WinForms application?

Should the SessionFactory be a singleton? Or can I create it every time? What about sessions and transactions?

Any help would be appreciated. Thanks

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

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

发布评论

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

评论(3

故事与诗 2024-08-26 19:03:17

会话工厂应该是单例的,因为创建它的成本很高。它是线程安全的,因此使用它不存在线程问题。

我在 winforms 应用程序中使用了每次会话会话模式,并且发现它运行良好。通过此模式,您可以使用同一个会话来执行一系列属于一起的操作。在我看来,winforms 应用程序中的对话可以粗略地映射到应用程序中的 GUI 操作。

对每个 GUI 操作使用新会话有助于保持会话足够小,以避免一级缓存中的许多实体出现性能问题,同时避免对每个操作使用单独的会话,这也会导致性能问题。

要实现此目的,您可以在处理 GUI 命令之前创建一个新会话,并在处理该命令后释放该会话。下面是一个例子,它使用一个类(PersistenceContext),该类在实例化时创建会话,并在释放会话时释放该会话。然后,此类可以创建使用当前会话的存储库。

public void SomeGuiEvent(...)
{
    using (var context = new PersistenceContext())
    {
        ProcessStuff(context);
    }
}

当然,还有许多其他选项可以实现这一点,但是无论您选择实现它,我都可以推荐在 Winforms 应用程序中使用 NHibernate 时使用每个会话会话模式。

The session factory should be a singleton since it is expensive to create. It is thread safe so there is no threading issue with using it.

I have used the session-per-conversation pattern in winforms applications, and have found it to work well. With this pattern you use the same session for a series of operations that belong together. As I see it, a conversation in a winforms app could, roughly, be mapped to a GUI operation in your application.

Using a new session for every GUI operation helps keep the session small enough to to avoid performance problems with to many entities in the first level cache, while at the same time avoiding using a separate session for every operation, which also can cause performance problems.

To implement this you then create a new session before handling the GUI command, and when the command has been handled you dispose the session. Below is an example of this, which uses a class (PersistenceContext) that creates a session when it is instantiated and disposes the session when it is disposed. This class can then create repositories that use the current session.

public void SomeGuiEvent(...)
{
    using (var context = new PersistenceContext())
    {
        ProcessStuff(context);
    }
}

There are ofcourse many other options of how to implement this, but however you choose to implement it, I can recommend the Session-per-conversation pattern when using NHibernate in a Winforms app.

旧情别恋 2024-08-26 19:03:17

下面是一篇 msdn 文章,展示了 Oren Eini (Ayende Rahien) 构建的示例应用程序: 构建使用 NHibernate 的桌面待办事项应用程序

Here is an msdn article showing sample application built by Oren Eini (Ayende Rahien): Building a Desktop To-Do Application with NHibernate

是你 2024-08-26 19:03:17

这个问题在 Stack Overflow 上已经被问过好几次了。

会话工厂应该是单例的,因为它们的创建成本很高。

对于会话,对于 NHibernate 和 WinForms 的“每个表单一个会话”方法似乎有一个粗略的共识。这意味着您应该在打开访问数据库的表单时打开会话,并在关闭表单时关闭会话。

看看这个问题的答案——
NHibernate 会话的生命周期应该是多长? - 一些更详细的描述,以及一些供进一步阅读的好链接。

This has been asked a few times on Stack Overflow.

The Session Factory should be a singleton, because they are expensive to create.

For Sessions, there seems to be a rough consensus on a "one session per form" approach for NHibernate and WinForms. This means you should open a session when you open a form that accesses your database, and close the session when you close the form.

Look at the answers to this question -
What should be the lifetime of an NHibernate session? - for some more detailed descriptions, and some good links for further reading.

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