English 中文(简体)
NHibernate session management in NSERBus with Autofac
原标题:NHibernate session management in NServiceBus with Autofac

Andreas Ohlund有一个很好的条款:here,内容是如何利用结构图对NHibernate session进行电解,从而自动加入国家会计准则理事会的交易。

是否有任何人知道是否有可能与自治州实现同样的目标?

问题回答

一位同事向我致了开幕词。

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()
    {
    }
}

public interface ISessionManager { ISession Session { get; } ISession OpenSession(); bool IsSessionOpened { get; } void CloseSession(); }

public class NHibernateSessionManager : ISessionManager { private ISessionFactory _sessionFactory; private 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;
    }
}

你确实与你提到的艺术一样,只是选择了一种自动生活镜。 如果你有其他班级的电文处理工作,请您向您介绍会,那么你就使用照相机。

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);
    }

}

你还可以在贵国家附属机构的范围内登记你所需要的任何其他附属设施,而且你将确保,由于使用儿童集装箱,这种设施能够即时使用并妥善存放。





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签