gpt4 book ai didi

c# - 多接口(interface)装饰器——Autofac 中的循环依赖之谜

转载 作者:行者123 更新时间:2023-11-30 15:59:03 25 4
gpt4 key购买 nike

我来自 Ninject,但我决定尝试一下 Autofac,因为它的开发似乎更加活跃。到目前为止,我可以说注册装饰器并不像在 Ninject 中使用 .WhenInjectedExactlyInto 那样容易。句法。无论如何,请耐心等待,因为我是 Autofac 新手。

问题是:

我有类型 A实现接口(interface) IAA_Decorator 装饰. A_Decorator实现接口(interface) IAIB ,然后应该由 AB_Decorator 装饰这也实现了 IAIB . AB_Decorator需要两个类型的依赖项 IAIB (所以它是两者的装饰器)但它们都应该解析为 A_Decorator 的同一个实例.它看起来像这样:AB_Decorator(A_Decorator(A) as IA, A_Decorator(A) as IB) .请求 IA 类型的服务时或输入 IB从 Autofac 容器中,它们应该引用单个 AB_Decorator实例。

用文字描述有点棘手,但这是我能想出的最简单的代码示例,它显示了这种情况(我已经向构造函数添加了实例 ID 和跟踪消息以查看发生了什么):

using System;
using Autofac;

namespace AutofacExample
{
internal interface IA { }

internal interface IB { }

class A : IA
{
static int _instanceCounter;
readonly int Id = ++_instanceCounter;

public A()
{
Console.WriteLine(this);
}

public override string ToString()
{
return $"{GetType().Name}[{nameof(Id)}={Id}]";
}
}

class A_Decorator : IA, IB
{
static int _instanceCounter = 10;
readonly int Id = ++_instanceCounter;

/* decorated1 should reference instance of A */

public A_Decorator(IA decoratedA)
{
Console.WriteLine($"{this}({nameof(decoratedA)}={decoratedA})");
}

public override string ToString()
{
return $"{GetType().Name}[{nameof(Id)}={Id}]";
}
}

class AB_Decorator : IA, IB
{
static int _instanceCounter = 100;
readonly int Id = ++_instanceCounter;

/* Both decorated1 and decorated2 should reference the same instance of A_Decorator */

public AB_Decorator(IA decoratedA, IB decoratedB)
{
Console.WriteLine($"{this}({nameof(decoratedA)}={decoratedA}, {nameof(decoratedB)}={decoratedB})");
}

public override string ToString()
{
return $"{GetType().Name}[{nameof(Id)}={Id}]";
}
}

class Program
{
static void Main(string[] args)
{
ContainerBuilder builder = new ContainerBuilder();

builder
.RegisterType<A>()
.Named<IA>(nameof(A))
.SingleInstance();

builder
.RegisterType<A_Decorator>()
.Named<IA>(nameof(A_Decorator))
.Named<IB>(nameof(A_Decorator))
.SingleInstance();

builder
.RegisterType<AB_Decorator>()
.Named<IA>(nameof(AB_Decorator))
.Named<IB>(nameof(AB_Decorator))
.SingleInstance();

/* A is decorated by A_Decorator as IA */
builder
.RegisterDecorator<IA>(
(c, decorated) =>
c.ResolveNamed<IA>(nameof(A_Decorator), TypedParameter.From(decorated)),
nameof(A))
//.Keyed<IA>("innerA")
//.Keyed<IB>("innerB")
.SingleInstance();

/* Trying to register AB_Decorator as IA creates circular dependency */
//builder
// .RegisterDecorator<IA>(
// (c, decorated) =>
// c.ResolveNamed<IA>(nameof(AB_Decorator), TypedParameter.From(decorated)),
// "innerA")
// .SingleInstance();

/* A_Decorator is decorated by AB_Decorator as IB */
builder
.RegisterDecorator<IB>(
(c, decorated) =>
c.ResolveNamed<IB>(nameof(AB_Decorator), TypedParameter.From(decorated)),
nameof(A_Decorator) /* "innerB" */)
.SingleInstance();

IContainer container = builder.Build();

IA a = container.Resolve<IA>();
IB b = container.Resolve<IB>();

Console.WriteLine($"{nameof(a)} == {nameof(b)} ? {ReferenceEquals(a, b)}");
Console.WriteLine($"{nameof(a)} is {a.GetType().Name}");
Console.WriteLine($"{nameof(b)} is {b.GetType().Name}");
}
}
}

不幸请求 IA 的实例给我A_Decorator , 而对于 IB我得到 AB_Decorator .尝试取消注释额外的装饰器注册 block 会导致循环依赖异常(DependencyResolutionException: Circular component dependency detected: System.Object -> AutofacExample.AB_Decorator -> System.Object -> AutofacExample.AB_Decorator),我无法尝试命名注册的各种组合。

有人知道解决这个问题的方法吗?提前致谢。

最佳答案

问题

问题在于 AB_Decorator 的装饰器注册。特别是解析 AB_Decorator 的 lambda 函数:

( c, decorated ) => c.ResolveNamed<IA>( nameof( AB_Decorator ), TypedParameter.From( decorated ) );

AB_Decorator 的构造函数接受 2 个参数,这两个参数应该是 A_Decorator 的同一个实例,它作为 decorated 提供给 lambda .但是,decorated 仅作为参数通过 TypedParameter.From( decorated ) 传递一次。因此 Autofac 将尝试通过容器解析第二个参数。

现在 IB 的注册显示我们应该得到一个 A_Decorator 包裹在 AB_Decorator 中的单例实例。所以要解析IB,容器必须构造AB_Decorator。问题来了,我们目前正在尝试将 AB_Decorator 解析为 IA,但我们需要一个 IB 来满足 的构造函数参数>AB_Decorator 正在为 IA 构建。而IB在容器中注册为AB_Decorator。所以你得到:

AB_Decorator(A_Decorator(A) as IA, AB_Decorator(A_Decorator(A) as IA, AB_Decorator(etc...))

解决方案

我们需要在解析AB_Decorator时将decorated传入两个参数。像这样:

builder
.RegisterDecorator<IA>(
( c, decorated ) =>

c.ResolveNamed<IA>( nameof( AB_Decorator ),
new TypedParameter( typeof( IA ), decorated ),
new TypedParameter( typeof( IB ), decorated )
)
,"innerA"
)
.SingleInstance();


builder
.RegisterDecorator<IB>(
( c, decorated ) =>

c.ResolveNamed<IB>( nameof( AB_Decorator ),
new TypedParameter( typeof( IA ), decorated ),
new TypedParameter( typeof( IB ), decorated )
)
, nameof( A_Decorator ) /* "innerB" */
)
.SingleInstance();

现在我们将 decorated 也就是 A_Decorator 发送给 IAIB 参数。直接构造 TypedParameter 实例允许我在参数列表中指定我希望实例实现的类型,在本例中为 AB_Decorator

关于c# - 多接口(interface)装饰器——Autofac 中的循环依赖之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42279567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com