Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I took a very similar approach. In the HttpModule I ask the sessionfactory for a new session + bind it when a new request comes in. But I also begin the transaction here. Then when the request is ending I simply unbind it and attempt to commit the transaction. </p> <p>Also my base repository doesn't take a session in any way - it instead will ask for the current session and then perform some work with the session. Also I don't wrap anything inside this base class with a transaction. Instead the entire http request is a single unit of work.</p> <p>This might not be appropriate for the project you are working on, but I prefer this approach because each request will fail or succeed as a single atomic unit. I have a full blog post <a href="http://toranbillups.com/blog/archive/2009/09/15/Transaction-management-with-NHibernate" rel="nofollow noreferrer">here</a> with source code if you are interested in the actual implementation. </p> <p>The below is a sample of what this base repository looks like:</p> <pre><code>public abstract class NHibernateRepository&lt;T&gt; where T : class { protected readonly ISessionBuilder mSessionBuilder; public NHibernateRepository() { mSessionBuilder = SessionBuilderFactory.CurrentBuilder; } public T Retrieve(int id) { ISession session = GetSession(); return session.Get&lt;T&gt;(id); } public void Save(T entity) { ISession session = GetSession(); session.SaveOrUpdate(entity); } public void Delete(T entity) { ISession session = GetSession(); session.Delete(entity); } public IQueryable&lt;T&gt; RetrieveAll() { ISession session = GetSession(); var query = from Item in session.Linq&lt;T&gt;() select Item; return query; } protected virtual ISession GetSession() { return mSessionBuilder.CurrentSession; } } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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