views:

106

answers:

1

Hi,

What is best pratices for inject and manage Session/Transaction for NHibernate using StructureMap for a Non Web Application like an Windows Service ?

In a web context, we use PerRequest Session management lifecycle using the Hybrid Lifecycle of StructureMap but for a Windows Service, i can't handle IDisposable UnitOfWork ...

Thanks.

A: 

Ayende gives a good summary of how to handle NHibernate in a desktop application here.

With StructureMap, I first create the ISessionFactory manually. Then declare the instance to be used on all ISessionFactory instances:

        For<ISessionFactory>().Use(sessionFactory);

Thus, you leave the Screens themselves to create and dispose of their sessions. It's important that all sessions are isolated from one another to avoid memory problems. This is explained in full in the article.

With this approach, and with the good use of the IStatelessSession as needed, I've cut down on a lot of the issues I have had in a WPF/StructureMap/NHibernate application.

Michael Hedgpeth