Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I implement NHibernate session per request without a dependency on NHibernate?
    text
    copied!<p>I've raised this question before but am still struggling to find an example that I can get my head around (please don't just tell me to look at the S#arp Architecture project without at least some directions).</p> <p>So far I have achieved near persistance ignorance in my web project. My repository classes (in my data project) take an ISession in the constructor:</p> <pre><code>public class ProductRepository : IProductRepository { private ISession _session; public ProductRepository(ISession session) { _session = session; } </code></pre> <p>In my global.asax I expose the current session and am creating and disposing session on beginrequest and endrequest (this is where I have the dependency on NHibernate):</p> <pre><code> public static ISessionFactory SessionFactory = CreateSessionFactory(); private static ISessionFactory CreateSessionFactory() { return new Configuration() .Configure() .BuildSessionFactory(); } protected MvcApplication() { BeginRequest += delegate { CurrentSessionContext.Bind(SessionFactory.OpenSession()); }; EndRequest += delegate { CurrentSessionContext.Unbind(SessionFactory).Dispose(); }; } </code></pre> <p>And finally my StructureMap registry:</p> <pre><code> public AppRegistry() { For&lt;ISession&gt;().TheDefault .Is.ConstructedBy(x =&gt; MvcApplication.SessionFactory.GetCurrentSession()); For&lt;IProductRepository&gt;().Use&lt;ProductRepository&gt;(); } </code></pre> <p>It would seem I need my own generic implementations of ISession and ISessionFactory that I can use in my web project and inject into my repositories?</p> <p><strong>So just to clarify - I am using NHibernate in my repository layer and want to use a session-per-(http)request. Therefore I am injecting an ISession into my repository constructors (using structuremap). Currently to create and dispose the sessions in each request I have had to reference NHibernate from my web project. This is the dependency I would like to remove.</strong></p> <p>Thanks, Ben</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