C# 在StructureMap中遍历依赖关系图
我在StructureMap中找不到任何遍历依赖关系图的机制。 我的问题如下: 类C# 在StructureMap中遍历依赖关系图,c#,.net,dependency-injection,structuremap,C#,.net,Dependency Injection,Structuremap,我在StructureMap中找不到任何遍历依赖关系图的机制。 我的问题如下: 类协调器有两个依赖项:ServiceA和ServiceB。 ServiceA和ServiceB都有一个依赖项:CustomWebClient。 CustomWebClient有一个依赖项:Logger 我必须将Logger的不同实现注入CustomWebClient。这取决于是将CustomWebClient注入ServiceA还是ServiceB 我发现的唯一条件机制是基于父类型和根类型的,但在我的情况下没有帮助。
协调器
有两个依赖项:ServiceA
和ServiceB
。
ServiceA
和ServiceB
都有一个依赖项:CustomWebClient
。
CustomWebClient
有一个依赖项:Logger
我必须将Logger
的不同实现注入CustomWebClient
。这取决于是将CustomWebClient
注入ServiceA
还是ServiceB
我发现的唯一条件机制是基于父类型和根类型的,但在我的情况下没有帮助。是否可以在StructureMap中配置此功能?虽然我没有找到任何方法以正常方式进行配置,但我发现StructureMap在内部存储依赖项堆栈 在StructureMap 2.6.4.0中,我们可以使用构建堆栈框架:
container.For<Logger>().AlwaysUnique().Use(ctx =>
{
var pi = typeof (BuildFrame).GetProperty("Parent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var frame = ctx.BuildStack.Parent;
while (frame != null)
{
var currentType = frame.ConcreteType;
if (currentType == typeof(ServiceA))
return new LoggerA();
if (currentType == typeof(ServiceB))
return new LoggerB();
frame = (BuildFrame)pi.GetValue(frame, null);
}
return new LoggerDefault();
}
container.For().AlwaysUnique().Use(ctx=>
{
var pi=typeof(BuildFrame).GetProperty(“父”,BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var frame=ctx.BuildStack.Parent;
while(帧!=null)
{
var currentType=frame.ConcreteType;
if(currentType==typeof(ServiceA))
返回新LoggerA();
if(currentType==typeof(ServiceB))
返回新的LoggerB();
frame=(BuildFrame)pi.GetValue(frame,null);
}
返回新的LoggerDefault();
}
在StructureMap 3.1.2.0中,我们必须使用
\u实例BuildSession
对象(IContext
实现)的私有字段.\u实例
字段具有以下类型:堆栈
应该能够创建一个新的CustomWebClientB
,它派生自依赖于LoggerB
的CustomWebClient
,然后有条件地使用它,如果ParentType
是ServiceB
。谢谢,但我真的不想这样做。t实际情况更复杂,我将其简化以表达问题的本质。在我们的项目中,有6种类型的服务。除此之外,CustomWebClient
有两个装饰器。我希望避免再创建18个类。是的,这改变了情况。