【问题标题】:CurrentSessionContext on NHibernateNHibernate 上的 CurrentSessionContext
【发布时间】:2012-08-01 08:29:33
【问题描述】:

我正在用多个会话工厂重构我们的 UnitOfWork 代码。我们的 UoW 正在遍历所有会话工厂,打开它们并绑定到上下文 - 这就是问题所在。

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

这个 UoW 必须是通用的 - 这意味着它可以被 WCF、Web 或单元测试使用。因此,对于 WCF 应用程序,我们在 nh 配置会话上下文中设置为“WcfOperationSessionContext”,例如 Web。 “ManagedWebSessionContext”。出现的问题是我们不能使用 CurrentSessionContext 类的静态方法,因为方法“Bind”需要 ISessionFactoryImplementor 从“CurrentSessionContext”继承。经过一番搜索,我们发现在示例中它直接调用在 nh 配置文件中设置的上下文,但在来自互联网的其他实现中,它们严格使用“CurrentSessionContext”。

这应该怎么做?如果我们应该直接调用在 nh config 中配置的“ICurrentSessionContext”的上下文实现,或者总是使用“CurrentSessionContext”(这不像我提到的那样工作)?

【问题讨论】:

    标签: c# nhibernate


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2013-12-14
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      相关资源
      最近更新 更多