我来自Ninject,但我决定尝试一下Autofac,因为它似乎更加活跃.到目前为止,我可以说注册装饰器并不像使用.WhenInjectedExactlyInto语法在Ninject中那样简单.无论如何,因为我是Autofac新手,所以请多多包涵.
这是问题所在:
我具有由A_Decorator装饰的类型A实现接口IA. A_Decorator实现接口IA和IB,并且依次应由同时实现IA和IB的AB_Decorator装饰. AB_Decorator具有IA和IB类型的两个依赖关系(因此,这是两者的装饰器),但是它们都应解析为A_Decorator的同一实例.看起来像这样:AB_Decorator(A_Decorator(A)如IA,A_Decorator(A)如IB).当从Autofac容器请求IA类型或IB类型的服务时,它们应引用单个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.尝试取消注释额外的装饰器注册块会导致循环依赖项异常(DependencyResolutionException:检测到循环组件依赖项: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个参数,这两个参数都应与作为装饰提供给lambda的A_Decorator实例相同.但是,仅通过TypedParameter.From(装饰)将装饰作为参数传递一次.因此,Autofac将尝试通过容器解析第二个参数.
现在,IB的注册表明,我们应该获得一个单例实例,其中A_Decorator包装在AB_Decorator中.因此,要解析IB,容器必须构造AB_Decorator.有一个问题,我们目前正在尝试将AB_Decorator解析为IA,但是我们需要一个IB来满足为IA构造的AB_Decorator的构造函数参数. IB在容器中注册为AB_Decorator.这样就得到:
AB_Decorator(A_Decorator(A) as IA, AB_Decorator(A_Decorator(A) as IA, AB_Decorator(etc...))
解
解析AB_Decorator时,我们需要将修饰符传递给这两个参数.像这样:
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();
现在,我们向IA和IB参数发送装饰性A_Decorator.直接构造TypedParameter实例使我可以在参数列表中为该实例指定AB_Decorator希望实例实现的类型.