开发者

GetCurrentSession Session Scoping for a Desktop app

I very recently discovered nhibernate session scoping and contexts. It seems that using this for a desktop app is as simple as bootstrapping the configuration with a ThreadStaticSessionContext, binding the session factory to the context, and then just calling sessioonFactory.GetCurrentSession() as needed.

I am looking for experienced 开发者_JAVA百科nhib devs that may have any opinions, tips, and/or links re:

(1) why this is or isn't a good approach for a desktop application

(2) unit testing with mocks

(3) what to do when you want a IStatelessSession

(4) how this scales when the app needs web functionality.


1) You probably want a little more flexibility in terms of the scope of your session. Take a look at What should be the lifetime of an NHibernate session? for some more info

2) You could unit test this code assuming you abstracted away the calls to sessionFactory, but, again, I think you should look into session scope a little more

3) That's a good question with this implementation

4) You will not be able to use a ThreadStaticSessionContext with web apps. You'll most likely want to use a Session-Per-Request for that, creating a new session at the beginning of a request and disposing it at the end.

I would recommend abstracting away the creation of the ISession using DI. If your code is calling XYZ.GetCurrentSession() before executing code, that's bad. If it's creating a new session (only to be used in the current context) that's fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜