views:

2333

answers:

1

Hi guys, I have a repository class that defines some basic Get/Save/Delete methods. Inside these, I use NHibernate to do the work on my business entities. For example:

Public Class SecurityRepositoryNHibImpl : Implements ISecurityRepository
    Public Function GetUser(ByVal UUID As System.Guid) As Entities.User Implements ISecurityRepository.GetUser
        Dim eUser As Entities.User
        Using session As ISession = NHibernateHelper.OpenSession()
            eUser = session.Get(Of Entities.User)(UUID)
        End Using
        Return eUser
    End Function
End Class

However, on my User class I have some properties and collections of other objects that ideally I'd like to be lazy loaded. But of course, the ISession is created and disposed within the repository, which I guess is why, outside of that, when I try to access those properties I get a "Could not initialize proxy - no Session" error.

Is my only option then to disable lazy loading when using repositories? Or is it possible (or just foolish) to somehow get the session into scope in the business layer?

I do like the repository model, and NHibernate is growing on me (after lots of initial frustration trying to make it work), so what's the best way you gurus have found of using them together?

I'm pretty new to NHibernate and repository models in general (at work we still mainly use VB6!), so forgive what may be a silly question. Thanks.


@mookid: Thanks dude, that's really helpful but I might leave it open a little longer. It's the back-end to a WCF web service, and all the functions will be per-call context, so a per-call session-life will be good. Just not sure how to get such a thing working in the business layer, ideally I don't want the business objects to have to interface directly with any NHibernate classes. I guess some kind of wrapper for the NHibernate session at least abstracts it away... Hmm, you've put me on the right track at least.


Looks like the key word here is "Unit of Work", for which a ton of resources exist on the web in relation to NHibernate. In particular look for Ayende's implementation in Rhino Commons, and his App Architecture web cast (no. 9 at Hibernating Rhinos), very informative. I was initially confused because I though putting a "Unit of Work" in the business layer was mixing concerns, but I was soon corrected.

+10  A: 

Creating your ISession and disposing it explicitly inside your repositories like that is ok for small and very simple projects - but, as you have found out, it comes short when you start wanting to use some of the cool features that NHibernate supports.

You should probably let the lifestyle of the session be controlled by something outside of your repositories - e.g. if you are doing ASP.NET web applications, you might want to save the session in the current request (HttpContext.Current.Items if I recall correctly) for the duration of the web request, and then commit and dispose at the end of the request (or rollback if an exception occurs).

I don't know the best way to control the lifestyle of your session in ASP.NET applications, but there must be some way to have code called at the beginning and at the end of each request. In ASP.NET MVC it can be easily accomplished by deriving all your controllers from a base controller that has overriden its OnActionExecuting and OnActionExecuted methods.

mookid8000