Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to use NHibernate with the repository model
    text
    copied!<p>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:</p> <pre><code>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 </code></pre> <p>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.</p> <p>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?</p> <p>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?</p> <p>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.</p> <hr> <p>@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.</p> <hr> <p>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 <a href="http://ayende.com/Blog/archive/2007/06/08/Rhino-Commons-RepositoryltTgt-and-Unit-Of-Work.aspx" rel="nofollow noreferrer">Ayende's implementation</a> in Rhino Commons, and his App Architecture web cast <a href="http://ayende.com/hibernating-rhinos.aspx" rel="nofollow noreferrer">(no. 9 at Hibernating Rhinos)</a>, very informative. I was initially confused because I though putting a "Unit of Work" in the business layer was mixing concerns, <a href="http://groups.google.com/group/nhusers/browse_thread/thread/6e8e76b08c05ebf" rel="nofollow noreferrer">but I was soon corrected.</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload