views:

23

answers:

3

Hello,

In Building a Desktop To-Do Application with NHibernate, Oren Eini (a.k.a. Ayende Rahien) shares that the general recommended NHibernate practice is to use one session per form in a desktop application. In the example given, this is easily implemented because the form presenters have knowledge of the persistence layer and so can create and dispose of sessions to match their life cycles.

In a more complex application, a service/business logic layer (BLL) usually sits between UI code and the persistence layer. The UI layer knows nothing about persistence (or sessions). In such a case, how does one (or does one?) maintain one session per form ?

Thank you,
Ben

A: 

The service layer will still get a session injected, either directly or by using a current session context.

Diego Mijelshon
Thanks for this answer. Do you have an example of how to do this that you can point me to? Keep in mind that the service layer I'm talking about is running in the same process as the UI (that is, it's not a web service).
Ben Gribaudo
Have a look at the examples in [unhaddins](http://code.google.com/p/unhaddins/)
Diego Mijelshon
A: 

You might think about using the MVC architecture, popular in web applications, where the controller deals directly with the domain model and the NHibernate sessions rather than delegating business logic to a business logic layer. The controller can maintain its own sessions however it likes.

Justice
A: 

It depends. In complex applications when using service oriented architectures the Nhibernate Session is generally tied to the service's session model.

You should decide wether the service layer will be requiring sessions to the desktop clients or not, and implement sessions in nhibernate following the service needs.

iCe