Nhibernate : Repository Session Management?

You should be using the "one session per request" pattern, by storing the ISession object in the HttpContext and sharing it between repositories and queries made during the same HTTP request.

You should be using the "one session per request" pattern, by storing the ISession object in the HttpContext and sharing it between repositories and queries made during the same HTTP request. Here's an implementation using MVC action attributes. An easy/basic implementation could also be achieved by simply altering your NbernateHelper class like this: public class N.. const string SessionKey = "NhibernateSessionPerRequest"; public static ISession OpenSession(){ var context = HttpContext.

Current; if(context! = null && context.Items. ContainsKey(SessionKey)){ //Return already open ISession return (ISession)context.

ItemsSessionKey; } else{ //Create new ISession and store in HttpContext var newSession = SessionFactory.OpenSession(); if(context! = null) context. ItemsSessionKey = newSession; return newSession; } } } Code hasn't been neither compiled nor tested... should work however.

Your code or, preferably, dependency injection should always pass the ISession into a repository's constructor. This allows multiple repositories to participate in a single transaction. I second Paco's recommendation to let a dependency injection framework handle this for you.

The challenge with this approach is with non-web applications that do not have clean unit-of-work boundaries like the HTTP request-response cycle. We have repositories that are shared by Windows Forms and ASP. NET applications and we manually manage newing up repositories in the Windows Forms applications.

I thought as much, i'm using ninject ( added code above) . How would I bind the session to CompanyRepository. Sorry this all hasn't clicked yet.

– frosty Jun 4 '10 at 12:08 Bind().To(). WithConstructorArgument("ses? Sion", NOpenSession()); There are probably other ways to do it.

– Jamie Ide Jun 4 '10 at 12:34.

Try using sessionFactory. GetCurrentSession() which will allow you to access a contextual session. This will basically allow you to use the 'session per request' model as described in another answer, without having to code that yourself.

You can even choose what your context is: Http (as your example suggests) or a bunch of others too (I use CallSessionContext for my unit test).

Your code or, preferably, dependency injection should always pass the ISession into a repository's constructor. This allows multiple repositories to participate in a single transaction.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions