zoukankan      html  css  js  c++  java
  • Yet another way to manage your NHibernate ISessionFactory

    So here is my current UnitOfWork implementation.  This one makes use of the somewhat new current_session_context_class feature. I think this is quite simple compared to some of the others you will find.

    public interface IUnitOfWork : IDisposable
        {
            IUnitOfWork Start();
            void BeginTransaction();
            void CommitTransaction();
            void RollbackTransaction();
        }
    public class UnitOfWork : IUnitOfWork
        {
            #region Dependencies
    
            public ISessionFactory SessionFactory
            {
                get;
                set;
            }
    
            #endregion
    
            #region IUnitOfWork Members
    
            public virtual void BeginTransaction()
            {
                var session = SessionFactory.GetCurrentSession();
                if ( !session.Transaction.IsActive )
                {
                    session.BeginTransaction();
                }
            }
    
            public virtual void CommitTransaction()
            {
                var session = SessionFactory.GetCurrentSession();
                if ( session.Transaction.IsActive )
                {
                    session.Transaction.Commit();
                }
            }
    
            public void RollbackTransaction()
            {
                var session = SessionFactory.GetCurrentSession();
                if ( session.Transaction.IsActive )
                {
                    session.Transaction.Rollback();
                }
            }
    
            public IUnitOfWork Start()
            {
                if ( !CurrentSessionContext.HasBind(SessionFactory) )
                {
                    var session = SessionFactory.OpenSession();
                    session.FlushMode = FlushMode.Commit;
                    CurrentSessionContext.Bind(session);
                }
                return this;
            }
    
            #endregion
    
            #region IDisposable Members
    
            public void Dispose()
            {
                var session = CurrentSessionContext.Unbind(SessionFactory);
                var transaction = session.Transaction;
                if ( transaction.IsActive )
                {
                    transaction.Dispose();
                }
                session.Dispose();
            }
    
            #endregion
        }

    All that’s required is this in the hibernate config section:

    (for web apps):

    <property name="current_session_context_class">web</property>

    (for pretty much anything else): 

    <property name="current_session_context_class">thread_static</property>

    and just the barebones bootstrapping:

    var sessionFactory = new Configuration().Configure().BuildSessionFactory();
                

    Of course, to get this to work you need to register that sessionFactory instance into your IoC container, and register the UnitOfWork type with the container with a transient lifecycle.

    From there you can either explicitly create and dispose IUnitOfWork instances for each operation, or set up a HttpModule to create one at the start of each request and dispose of it in the end_request event.

  • 相关阅读:
    排列 POJ
    [kuangbin带你飞]专题二十一 概率&期望 部分题解
    队列最大值&滑动窗口最大值【剑指offer】
    Python实现可视化界面多线程豆瓣电影信息爬虫,并绘制统计图分析结果
    剑指offer【复杂链表的复制】
    树上博弈——从根节点先后走向叶子节点输赢【递归水题】
    给一个长度为n的字符串,找出长度为m的最小字典子序列【单调栈】
    Hrbust 1814 小乐乐的化妆品【01背包】
    Hrbust 1541集合划分 & Hrbust 2002幂集【dp】
    Hrbust 1333 GG的关心【01背包】
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/4629904.html
Copyright © 2011-2022 走看看