如何在WPF应用程序中确定NHibernate会话和事务的范围 [英] How to scope NHibernate sessions and transactions in a WPF application

查看:67
本文介绍了如何在WPF应用程序中确定NHibernate会话和事务的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WPF和NHibernate都比较陌生,并尝试使用MVVM模式构建将两者结合在一起的应用程序.但是,我很难理解我的应用程序应在何时何地打开和关闭NHibernate会话和事务.

I am relatively new to both WPF and NHibernate and attempting to build an application that combines the two, using the MVVM pattern. However, I am struggling to understand when and where my application should open and close NHibernate sessions and transactions.

从我阅读的内容来看,建议会话应保持尽可能短的时间.因此,除了明显的线程安全性考虑之外,我认为在应用程序级别打开会话并共享该会话是不正确的吗?另一方面,我也意识到很短的会话(在业务方法内进行)会导致实体分离,也阻止了我利用NHibernate的延迟加载.这是设计WPF/NH应用程序的正常且不可避免的方法吗?

From what I have read, it is recommended that sessions should be kept as short as possible. Therefore, aside from the obvious thread-safety considerations, I assume it is not correct to open a session at the application level and share it around? On the other hand, I also realize that very short sessions (scoped within business methods) will result in detached entities and also prevent me from exploiting NHibernate's lazy loading. Is this a normal and unavoidable way to design a WPF/NH application?

不幸的是,所有好的代码示例(以及我可以在该主题上找到的唯一的书-NHibernate in Action)都集中在ASP.NET应用程序上,其中最佳实践显然是很明确的-会话和事务跨度恰好一个Http请求.

Unfortunately all the good code samples (and the only book I can find on this topic - NHibernate in Action) are focused towards ASP.NET applications, where the best practice is apparently fairly clear cut - a session and a transaction span exactly one Http request.

因此,我希望得到WPF开发人员的一些指导,以帮助我了解WPF中的会话管理(理想情况下是为我提供一些好的代码示例).

So, I would appreciate some guidance from WPF developers, to help me to understand session management in WPF (ideally pointing me to some good code samples).

非常感谢,

蒂姆

推荐答案

这是我所知道的最好的NHibernate桌面应用程序示例之一(我相信它是为WPF编写的):

Here is one of the best NHibernate desktop application samples that I am aware of (I believe it was written with WPF in mind):

使用NHibernate构建桌面待办事项应用程序

这篇关于如何在WPF应用程序中确定NHibernate会话和事务的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆