XSockets.Net - how to manage NHibernate Session Context

别等时光非礼了梦想. 提交于 2019-12-25 03:24:33

问题


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 :)


回答1:


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.




回答2:


Since XSockets has state is will be bad for your database if you open the connection in the OnOpen event since the connection will remain open as long as the socket is open. Best is to use the repository only in the methods calling the CRUD operations as briefly as possible.

To get the instance of your repository should not be a bottleneck in this case.

I will be happy to review any code you might have.

Regards Uffe



来源:https://stackoverflow.com/questions/25583302/xsockets-net-how-to-manage-nhibernate-session-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!