Should a NHibernate desktop app use a SessionFactory and multiple sessions ? Does the One session per transaction rule apply only to web a开发者_JS百科pplications ?
Regards, MadSeb
Yes, an NHibernate desktop application should typically use multiple sessions.
In a two-tier scenario, you'll use a session per logical "session of interaction," such as a given "view" or "screen." Here you can maintain the session longer than you could in a web application, and reap the full benefit of lazy-loading, but at some point you want the user to "save" or "cancel" and move on to something else, and this is often a good place to end the session.
Using a single session throughout the application can cause a lot of stuff to be cached, and the client-side data may become stale or you could run into concurrency problems.
Furthermore, once a session encounters an exception, it is no longer valid and you'll have to abandon/reset whatever you were doing. If that is one form, it isn't a big deal, but if you have lots of objects throughout your application referencing the same session, they'll all be in a compromised state.
Yes. A session should be as short lived as possible, since it is seen as a unit-of-work.
Therefore, I would create a session per 'use case'. (A use-case can span multiple forms).
精彩评论