I'm refactoring our code of UnitOfWork with multiple sessions factory. Our UoW is iterating over all session factories opens them and bind to context - and here is the question.
public void InitializeSessions()
{
foreach (ISessionFactory sessionFactory in _sessionFactories)
{
if ( NHibernate.Context.CurrentSessionContext.HasBind(sessionFactory))
{
continue;
}
ISession session = sessionFactory.OpenSession();
NHibernate.Context.CurrentSessionContext.Bind(session);
session.BeginTransaction();
}
}
This UoW has to be universal - this mean that it can be used by WCF, Web or in unit tests. So for WCF apps we set in nh config session context to "WcfOperationSessionContext", for Web eg. "ManagedWebSessionContext". Problem which occurred is that we cannot use static method of class CurrentSessionContext, because method "Bind" need that ISessionFactoryImplementor inherit from "CurrentSessionContext". After some searching we found that in examples it calls directly this context which is set in nh config file but in other implementations from internet they are using strictly "CurrentSessionContext".
How really this should be done? If we should call directly context implementation of "ICurrentSessionContext" configured in nh config or always use "CurrentSessionContext" (this not work like I mentioned)?