Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your NH Session is a Unit of Work already <a href="http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx" rel="nofollow">http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx</a></p> <p>So I'm not sure why you would ever need to abstract this out any further. (if anyone reading this answer know's why I would be happy to hear, I've honestly never heard of any reason why you would need to...)</p> <p>I would implement a simple Session Per Request. I don't know how you would do that with Windsor since I've never used it, but with It's rather simple with StructureMap.</p> <p>I wrap the structuremap factory to hold my session factory and inject the session into the repositories as required.</p> <pre><code> public static class IoC { static IoC() { ObjectFactory.Initialize(x => { x.UseDefaultStructureMapConfigFile = false; // NHibernate ISessionFactory x.ForSingletonOf&lt;ISessionFactory>() .Use(new SessionFactoryManager().CreateSessionFactory()); // NHibernate ISession x.For().HybridHttpOrThreadLocalScoped() .Use(s => s.GetInstance&lt;ISessionFactory>().OpenSession()); x.Scan(s => s.AssembliesFromApplicationBaseDirectory()); }); ObjectFactory.AssertConfigurationIsValid(); } public static T Resolve&lt;T>() { return ObjectFactory.GetInstance&lt;T>(); } public static void ReleaseAndDisposeAllHttpScopedObjects() { ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects(); } } </code></pre> <p>In the global.asax file on Request_End I call the ReleaseAndDisposeAllHttpScopedObjects() method.</p> <pre><code> protected void Application_EndRequest(object sender, EventArgs e) { IoC.ReleaseAndDisposeAllHttpScopedObjects(); } </code></pre> <p>So the session is opened when I call my first repository, and when the request is ended it's disposed of. The repositories have a constructor which takes ISession and assigns it to a property. Then I just resolve the repo like:</p> <pre><code>var productRepository = IoC.Resolve&lt;IProductRepository&gt;(); </code></pre> <p>Hope that helps. There are many other ways of doing it, this is what works for me.</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