Ryan Lanciaux

Random programming blog.

NHibernate in an ASP.NET MVC Application

I've recently become interested in using NHibernate for some of my data access. For the past year or so I have been using SubSonic for that, however, I'm trying to take a bit more of a domain driven approach on the project I'm currently messing around on. Although I've only been looking at it for a couple of days, one thing I'm having a little bit of trouble figuring out is where my ISession should be opened / closed in an ASP.NET MVC application. Currently, I'm doing this in the Controller but I'm not totally sure that is the right way to go…

        public ActionResult ViewProduct(string ID)


            using (ISession session = SessionManager.GetCurrentSession())


                SimpleProductRepository repo = new SimpleProductRepository(session);


                return RenderView("ViewProduct",

                    new SimpleProductRepository(session).GetByTitle(ID));



What is the best way to go about this? Also, should I be looking more at using a Unit of Work pattern to achieve this?