I struggled with NHibernate session management and now I got two possible solutions to satisfy a session on a web request.
I am using Windsor for IoC in an ASPNET mvc project
The first solution is to open the session in begin_request and close / delete it again in end_request. In Windsor setup, I would have container.Register (Component.For (). Using FactMethod (() => SessionFactory.GetCurrentSession ()). LifeStyle.Transient;
This solution creates a session for the request and shares it through GetCurrentSession.
The second solution is to use Windsor as
container.Register (Component.For (). UsingFactoryMethod (() => SessionFactory.OpenSession ()). LifeStyle.PerWebRequest);
This will also give me a session for every request and constructor support. This is a bit more simpel, but I need a second opinion.
Please let me know what you prefer to use,
Regards Rasmus
source
share