views:

299

answers:

1

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  A: 

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

Michael Maddox
Many thanks Michael. As you say, this excellent article covers exactly the issues that I am facing.
Tim Coulter