Note that there are some explanatory texts on larger screens.

plurals
  1. POControlling NHibernate ITransaction with StructureMap?
    primarykey
    data
    text
    <p>I'm using StructureMap as my IoC container and NHibernate as my ORM. I found an example online that shows how to have StructureMap build the ISessionFactory and the ISession so the Factory is a singleton and the Session is based on the HttpContext. This works great, but then I started using NH Profiler which told me I should always be explicitly using Transactions. So, I thought, why not let StructureMap handle that for me too? Using the code below, I got that all working fine, except, I don't know how/where to commit/rollback my transaction.</p> <p>Here is how I initialize StructureMap:</p> <pre><code> ObjectFactory.Initialize(x =&gt; { x.ForRequestedType&lt;ISessionFactory&gt;() .CacheBy(InstanceScope.Singleton) .TheDefault.Is.ConstructedBy(cfg.BuildSessionFactory); x.ForRequestedType&lt;ISession&gt;() .CacheBy(InstanceScope.Hybrid) .TheDefault.Is.ConstructedBy(context =&gt; context.GetInstance&lt;ISessionFactory&gt;().OpenSession()); x.ForRequestedType&lt;ITransaction&gt;() .CacheBy(InstanceScope.Hybrid) .TheDefault.Is.ConstructedBy(context =&gt; context.GetInstance&lt;ISession&gt;().BeginTransaction()); x.Scan(y =&gt; { y.TheCallingAssembly(); y.WithDefaultConventions(); }); }); </code></pre> <p>All my repositories look like the following:</p> <pre><code> public UserRepository(ISession session, ITransaction transaction) { _session = session; _transaction = transaction; } </code></pre> <p>And a typical method inside a repository looks like:</p> <pre><code> public void Remove(int id) { Remove(_session.Get&lt;User&gt;(id)); } </code></pre> <p>What I'm trying to do is have all the methods that I call in one HttpContext share the same Session and Transaction. Is this possible or is this totally wrong and I'm barking up the wrong tree?</p> <p>Thanks in advance!</p> <p>-Dan</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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