Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to open and close the NHibernate ISession in a web app (specifically MVC)?

This is a pretty fundamental question when using NHibernate in a web application, but I don't see any agreed best practice when searching the web. I've seen it done in lots of different places:

Created and disposed in the Repository method - This just seems silly to me, since when you get the object it's already detached.

At the beginning and end of the Controller Action - This seems better, but annoying to have to do it for each action.

At the Application level, in global.asax beginrequest and endrequest - This seems the best idea, but again, I've seen some examples creating in Init instead of beginrequest (sharp architecture for instance) - although I am not sure why.

Maybe there are other approaches?
Can IoC containers help in some way here?
Maybe you know of a good resource on the web regarding this?
And - what method do you use?

Thanks

like image 637
UpTheCreek Avatar asked Nov 10 '09 19:11

UpTheCreek


1 Answers

Session per Request is probably the most used approach.

like image 53
Darin Dimitrov Avatar answered Oct 17 '22 06:10

Darin Dimitrov