views:

43

answers:

1

Hello

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 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 fuctionality.

Cheers,
Berryl

+1  A: 

1) You probably want a little more flexibility in terms of the scope of your session. Take a look at http://stackoverflow.com/questions/2013467/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.

statichippo
Thanks for the reply - some good info in that other SO question. Still leaves me suspecting there may be more to contexts and GetCurrentSession in general, and ThreadStaticContext in particular. Cheers
Berryl