【问题标题】:NHibernate session management in NServiceBus with AutofacNServiceBus 中的 NHibernate 会话管理与 Autofac
【发布时间】:2010-07-12 17:18:15
【问题描述】:

Andreas Ohlund 有一篇出色的文章 here,介绍了如何使用 Structuremap 连接 NHibernate 会话,以便它自动加入 NSB 事务。

有谁知道 Autofac 是否可以达到同样的效果?

【问题讨论】:

    标签: nhibernate nservicebus autofac


    【解决方案1】:

    一位同事给了我遮阳篷

    public class NHibernateMessageModule : IMessageModule
    {
        /// <summary>
        /// Injected SessionManager.
        /// </summary>
        public ISessionManager SessionManager { get; set; }
    
        public void HandleBeginMessage()
        {
            //this session need for NServiceBus and for us
            ThreadStaticSessionContext.Bind(SessionManager.OpenSession()); //CurrentSessionContext or  ThreadStaticSessionContext
        }
    
        public void HandleEndMessage()
        {
            SessionManager.Session.Flush();
        }
    
        public void HandleError()
        {
        }
    }
    

    公共接口ISessionManager { ISession 会话 { 获取; } ISession OpenSession(); 布尔 IsSessionOpened { 获取; } 无效关闭会话(); }

    公共类 NHibernateSessionManager : ISessionManager { 私有 ISessionFactory _sessionFactory; 私有ISession_session;

        public ISession Session
        {
            get { return _session; } 
            private set { _session = value; }
        }
    
        public SchemaExport SchemaExport { get; set; }
    
    
        public NHibernateSessionManager(ISessionFactory sessionFactory)
        {
            _sessionFactory = sessionFactory;
        }
    
        public bool IsSessionOpened
        {
            get { return Session != null && Session.IsOpen; } 
        }
    
        public ISession OpenSession()
        {
            if(Session == null)
            {
                Session = _sessionFactory.OpenSession();
                if (SchemaExport != null)
                    SchemaExport.Execute(true, true, false, Session.Connection, null);
            }
            return Session;
        }
    
        public void CloseSession()
        {
            if (Session != null && Session.IsOpen)
            {
                Session.Flush();
                Session.Close();
            }
            Session = null;
        }
    }
    

    【讨论】:

    • 这些不会在你的系统中引入奇怪的锁吗?
    【解决方案2】:

    您的操作与您提到的文章中的完全相同,但选择了 Autofac lifescopes 之一。如果您希望在其中注入会话的消息处理中涉及其他类,则可以像这样使用 InstancePerLifetimeScope

    public class EndpointConfig : IConfigureThisEndpoint, AsA_Publisher, IWantCustomInitialization
    {
    
        public void Init()
        {
            var builder = new ContainerBuilder();
            builder.Register(s => SessionFactory.CreateSessionFactory()).As<ISessionFactory>().SingleInstance();
            builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).As<ISession>().InstancePerLifetimeScope();
    
            var container = builder.Build();
            Configure.With().AutofacBuilder(container);
        }
    
    }
    

    您还可以在 NSB 上下文中注册所需的任何其他依赖项,并且由于使用了子容器,您将确保它被正确实例化和处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多