How to scope NHibernate sessions and transactions in a WPF application

☆樱花仙子☆ 提交于 2019-12-22 03:28:42

问题


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.

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?

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.

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).

Many thanks,

Tim


回答1:


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

Building a Desktop To-Do Application with NHibernate



来源:https://stackoverflow.com/questions/2253025/how-to-scope-nhibernate-sessions-and-transactions-in-a-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!