How to retrieve current NHibernate Session object from Castle ActiveRecord SessionScope
-
20-02-2021 - |
Question
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
Solution
I think that the Activerecord SessionScope can handle multiple factories for different types, but if you are not utilizing that I think you would be good to pass in any type of class that is a persisted one:
ISession session = SessionScope.Current.GetSession( typeof ( YourClass ) );
OTHER TIPS
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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow