使用 Autofac 管理 NHibernate ISession [英] Managing NHibernate ISession with Autofac

查看:27
本文介绍了使用 Autofac 管理 NHibernate ISession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 Autofac 如何帮助管理 NHibernate ISession 实例(在 ASP.NET MVC 应用程序的情况下),是否有人有任何提示或最佳实践?

Does anyone have any tips or best practices regarding how Autofac can help manage the NHibernate ISession Instance (in the case of an ASP.NET MVC application)?

推荐答案

我不太熟悉 NHibernate 会话应该如何处理.也就是说,Autofac 具有出色的实例生命周期处理(范围确定性处置).一些相关资源是这篇文章这个问题.由于您在 ASP.Net MVC 领域,请确保您还查看 MVC 集成内容.

I'm not overly familiar with how NHibernate sessions should be handled. That said, Autofac have excellent instance lifetime handling (scoping and deterministic disposal). Some related resources are this article and this question. Since you're in ASP.Net MVC land make sure you also look into the MVC integration stuff.

为了说明这一点,这里有一个关于如何使用 Autofac 工厂委托和 Owned 泛型来完全控制实例生命周期的快速示例:

To illustrate the point, here's a quick sample on how you can use Autofac factory delegates and the Owned generic to get full control over instance lifetime:

public class SomeController
{
    private readonly Func<Owned<ISession>> _sessionFactory;

    public SomeController(Func<Owned<ISession>> sessionFactory)
    {
        _sessionFactory = sessionFactory;
    }

    public void DoSomeWork()
    {
        using (var session = _sessionFactory())
        {
             var transaction = session.Value.BeginTransaction();
             ....   
        }
    }
}

使其工作的容器设置非常简单.请注意,我们无需执行任何操作即可获取 Func<>Owned<> 类型,它们由 Autofac 自动提供:

The container setup to get this to work is quite simple. Notice that we don't have to do anything to get the Func<> and Owned<> types, these are made available automatically by Autofac:

builder.Register(c => cfg.BuildSessionFactory())
    .As<ISessionFactory>()
    .SingleInstance();
builder.Register(c => c.Resolve<ISessionFactory>().OpenSession());

更新:我的理由是,根据 这个 NHibernate 教程,会话实例的生命周期应该是工作单元"的生命周期.因此,我们需要某种方式来控制会话实例的创建时间和会话的处理时间.

Update: my reasoning here is that, according to this NHibernate tutorial, the lifetime of the session instance should be that of the "unit of work". Thus we need some way of controlling both when the session instance is created and when the session is disposed.

使用 Autofac,我们通过请求 Func<> 而不是直接请求类型来获得这种控制.不使用 Func<> 将要求在创建控制器实例之前预先创建会话实例.

With Autofac we get this control by requesting a Func<> instead of the type directly. Not using Func<> would require that the session instance be created upfront before the controller instance is created.

接下来,Autofac 中的默认设置是实例具有其容器的生命周期.因为我们知道我们需要在工作单元完成后立即处理这个实例,所以我们请求一个 Owned 实例.在这种情况下,处置拥有的实例将立即处置基础会话.

Next, the default in Autofac is that instances have the lifetime of their container. Since we know that we need the power to dispose this instance as soon as the unit of work is done, we request an Owned instance. Disposing the owned instance will in this case immediately dispose the underlying session.

这篇关于使用 Autofac 管理 NHibernate ISession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆