使用 Autofac 返回不同作用域中的不同组件



>我有两个作用域,一个嵌套在另一个作用域内。当我解析特定服务时,我希望在一个根作用域中解析一个组件,在子作用域中解析另一个组件。有没有简单的方法可以做到这一点?

我已经设法使用工厂类确定当前范围然后返回适当的实例来工作:

IContainer BuildContainer()
{
    var builder = new ContainerBuilder();
    // ...
    builder.RegisterType<FooInParentScope>().AsSelf();
    builder.RegisterType<FooInChildScope>().AsSelf();
    builder.RegisterType<FooFactory>().AsImplementedInterfaces();
    builder.Register<IFoo>(c => c.Resolve<IFooFactory>().GetFoo()).InstancePerLifetimeScope();
    // ...
}

class FooFactory : IFooFactory
{
    private readonly ILifetimeScope m_scope;
    public FooFactory(ILifetimeScope scope)
    {
        m_scope = scope;
    }
    public IFoo GetFoo()
    {
        if (m_scope.Tag == "ParentScope")
            return m_scope.Resolve<FooInParentScope>();
        else
            return m_scope.Resolve<FooInChildScope>();
    }
}
class FooInParentScope : IFoo
{
}
class FooInChildScope : IFoo
{
}

这种方法存在许多问题:

  1. 我必须添加一个额外的类(或 2 个 - 不确定是否真的需要 IFooFactory)
  2. 上面的代码无法处理嵌套在 ParentScope 中的其他作用域。我可以通过将范围转换为Autofac.Core.Lifetime.LifetimeScope并检查ParentLifetimeScope属性来解决此问题,但这可能不是特别安全的做法。

您可以在根容器中将 FooInParentScope 注册为 SingleInstance。在创建内部生命周期范围时,将FooInChildScope的注册添加为SingleInstance(覆盖注册)。

builder.RegisterType<FooInParentScope>().As<IFoo>.SingleInstance();
var container = builder.Build();
var childScope = container.BeginLifetimeScope(innerBuilder =>
    {
        // override registration for IFoo in child lifetime scope:
        innerBuilder.RegisterType<FooInChildScope>().As<IFoo>().SingleInstance();
    });
FooInParentScope fooInParentScope = (FooInParentScope) container.Resolve<IFoo>();
FooInChildScope fooInChildScope = (FooInChildScope) childScope.Resolve<IFoo>();

最新更新