3

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)?

4

1 回答 1

2

对于 CurrentSessionContext,您想使用 WebSessionContext 而不是 ManagedWebSessionContext,根据文档:http ://nhibernate.info/doc/nh/en/index.html#architecture-current-session

于 2012-09-10T21:55:39.030 回答