XSockets.Net - how to manage NHibernate Session Co

2019-09-10 15:56发布

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

2条回答
孤傲高冷的网名
2楼-- · 2019-09-10 16:04

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

查看更多
ゆ 、 Hurt°
3楼-- · 2019-09-10 16:12

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.

查看更多
登录 后发表回答