nhibernatexsockets.net

XSockets.Net - how to manage NHibernate Session Context


I wonder what is the best way to manage NHibernate Session Context when using NH data layer from Xsockets controller. Particularly I refer to self hosted winservice/console application or Azure worker role, where HTTPContext is not available. Of course there is always an option to create and dispose session per call, but that means a performance hit, so better reuse sessions in some way. My controller provides API for CRUD operations in underlying NH repository and is pushing updates to relevant subscribers when certain records are updated in DB.

Your ideas appreciated :)


Solution

  • I'm using StructureMap to handle dependencies and create a NestedContainer to handle session per request. Don't have to mess with CurrentSessionContext or HttpContext anymore for storing session.

    http://structuremap.github.io/the-container/nested-containers/

    You could even just create a middleware UnitOfWork if you are using OWIN with WebAPI.