Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve current NHibernate Session object from Castle ActiveRecord SessionScope

I have an open Castle ActiveRecord SessionScope. I need to use the nhibernate session wrapped into SessionScope.

How can i retrieve the current NHibernate Session Object from SessionScope?

thank you very much for the replies.

[update] i have this code

    ISession session = SessionScope.Current.GetSession( );

but i don't know what pass to the GetSession parameter

like image 655
manuellt Avatar asked Nov 03 '11 11:11

manuellt


1 Answers

I resolved it with this code:

        ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
        ISessionScope activeScope = holder.ThreadScopeInfo.GetRegisteredScope();
        ISession session = null;
        var key = holder.GetSessionFactory(typeof(ActiveRecordBase));
        if (activeScope == null)
        {
            session = holder.CreateSession(typeof(ActiveRecordBase));
        }
        else
        {
            if (activeScope.IsKeyKnown(key))
                session = activeScope.GetSession(key);
            else
                session = holder.GetSessionFactory(typeof(ActiveRecordBase)).OpenSession();
        }
like image 132
manuellt Avatar answered Oct 03 '22 08:10

manuellt