Ryan LanciauxNew Media Mercenary

NHibernate in an ASP.NET MVC application

April 29, 2008 by ryan

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?
Tags:
Categories: Development
Actions: E-mail | Kick it! | Permalink | commentComments (4) | RSS comment feedComment RSS


Related posts

Comments

May 2. 2008 07:51

Matt Hinze

You want this to be transparent to your controller...

Use something like this

www.codeproject.com/.../...rnateBestPractices.aspx

or Jeffrey Palermo's HybridSessionBuilder w/ Session per request

palermo.googlecode.com/.../...nateSessionModule.cs

or Unit Of Work

rhino-tools.svn.sourceforge.net/.../...lication.cs


Matt Hinze

May 2. 2008 08:18

Ryan Lanciaux

@Matt: Thank you for the links! I will have to play around w/ these options a little more but they look great -- I knew how I was using the session was less than ideal.

Ryan Lanciaux

January 26. 2009 14:51

pingback

Pingback from sdesmedt.wordpress.com

NHibernate and ASP.Net: what’s the problem? « Hungry for Knowledge

sdesmedt.wordpress.com

January 31. 2009 15:37

Ali Özgür

You would like to check out this post blog.pragmasql.com/.../...Burrow-contribution.aspx

Ali Özgür

Comments are closed




© 2008 Ryan Lanciaux :: powered by BlogEngine.NET