Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scope NHibernate sessions and transactions in a WPF application

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

like image 637
Tim Coulter Avatar asked Feb 12 '10 15:02

Tim Coulter


1 Answers

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

like image 66
Michael Maddox Avatar answered Oct 23 '22 05:10

Michael Maddox