<p>I'm new to NHibernate (my 1st big project with it).<br>
I had been using a simple method of data access by creating the ISession object within a using block to do my grab my Object or list of Objects, and in that way the session was destroyed after exiting the code block.  </p>

<p>This doesn't work in a situation where lazy-loading is required, however.<br>
For example, if I have a Customer object that has a property which is a collection of Orders, then when the lazy-load is attempted, I get a Hibernate exception.<br>
Anyone using a different method?</p>

[{'answer_id': 32623, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 1, 'selected': False, 'text': '<p>Keep your session open for your entire unit of work.  If your session is life is too small, you cannot benefit from the session level cache (which is significant).  Any time you can prevent a roundtrip to the database is going to save a lot of time.   You also cannot take advantage of lazy loading, which is crucial to understand.</p>\n\n<p>If your session lifetime is too big, you can run into other issues.</p>\n\n<p>If this is a web app, you\'ll probably do fine with the session-per-httpRequest pattern.  Basically this is an HttpModule that opens the session at the beginning of the request and flushes/closes at the end.  Be sure to store the session in HttpContext.Items  NOT A STATIC VARIABLE.  &lt;--- leads to all kinds of problems that you don\'t want to deal with.</p>\n\n<p>You might also look at <a href="http://ayende.com/wiki/Rhino+Commons.ashx" rel="nofollow noreferrer">RhinoCommons</a> for a unit of work implementation.</p>\n'}, {'answer_id': 32624, 'author': 'Chris Conway', 'author_id': 2849, 'author_profile': 'https://Stackoverflow.com/users/2849', 'pm_score': 2, 'selected': False, 'text': '<p>check out the <a href="http://www.SummerOfNHibernate.com" rel="nofollow noreferrer">SummerOfNHibernate</a> webcasts for a great tutorial... What you\'re looking for specifically doesn\'t come until webisode 5 or 6.</p>\n'}, {'answer_id': 33622, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 4, 'selected': True, 'text': '<p>Session management:</p>\n\n<p><a href="http://code.google.com/p/dot-net-reference-app/source/browse/trunk/src/Infrastructure/Impl/HybridSessionBuilder.cs" rel="nofollow noreferrer">http://code.google.com/p/dot-net-reference-app/source/browse/trunk/src/Infrastructure/Impl/HybridSessionBuilder.cs</a></p>\n\n<p>Session per request:</p>\n\n<p><a href="http://code.google.com/p/dot-net-reference-app/source/browse/trunk/src/Infrastructure/Impl/NHibernateSessionModule.cs" rel="nofollow noreferrer">http://code.google.com/p/dot-net-reference-app/source/browse/trunk/src/Infrastructure/Impl/NHibernateSessionModule.cs</a></p>\n'}, {'answer_id': 52514, 'author': 'huseyint', 'author_id': 39, 'author_profile': 'https://Stackoverflow.com/users/39', 'pm_score': 0, 'selected': False, 'text': '<p>Since you are developing a Web App (presumably with ASP.NET), check out <a href="http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx" rel="nofollow noreferrer">NHibernate Best Practices with ASP.NET</a> at CodeProject.</p>\n'}]
['https://Stackoverflow.com/questions/32612', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1284/']