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

查看:76
本文介绍了使用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天全站免登陆