我正在处理遗留应用程序,我当前的目标是设置一些也使用结构图的集成测试。到目前为止,我使用 SM 进行单元测试和生产代码已经将近一年没有问题。但不幸的是,我们被迫使用 setter 注入(inject),因为我们的 BO 是真正的依赖野兽(很多这些对象对其他对象有循环依赖)
我们为此采用了两步法:我们是否首先创建所有这些对象而不注入(inject)属性(这会导致循环依赖),然后在它们创建时构建这些对象(通过它们的属性注入(inject)依赖对象)已经创建。 (参见具有循环依赖性的示例类 A、B 和逻辑)
到目前为止一切正常 -> 当我们依次运行 3 个集成测试时,除了第一个“变绿”之外,另一个测试在 structuremap 尝试创建实例(在 GetManagerInstances 中)方法时抛出异常。
我猜 SM 将其属性注入(inject)配置保持为静态。
定义
internal interface IM { }
internal interface ILogic { }
internal interface IMA : IM { }
internal interface IMB : IM { }
internal class A : IMA
{
public A() { Console.WriteLine("A"); }
public IMB BInstance { get; set; }
}
internal class B : IMB
{
public B() { Console.WriteLine("B"); }
public IMA AInstance { get; set; }
public ILogic Logic { get; set; }
}
internal class Logic : ILogic
{
public Logic() { Console.WriteLine("Logic"); }
public IMA AInstance { get; set; }
}
初始化
private static IContainer _container;
while (true)
{
Initialize();
}
internal static void Initialize()
{
_container = new Container(c =>
{
c.For<ILogic>().Singleton().Use<Logic>();
c.For<IMB>().Singleton().Use<B>();
c.For<IMA>().Singleton().Use<A>();
});
// All managers are created and afterwards properties are set to avoid a circular build stack.
List<object> managerInstances = GetManagerInstances();
// After the manager instances are created the dependencies are injected in the properties.
BuildUpManagersUsingPropertyInjection(managerInstances);
}
private static List<object> GetManagerInstances()
{
List<object> managerInstances = new List<object>();
foreach (Type pluginType in new List<Type> { typeof(IMA), typeof(IMB) })
{
try
{
managerInstances.Add(_container.GetInstance(pluginType));
}
catch (Exception ex)
{
// exception on second test run -> see below
}
}
return managerInstances;
}
private static void BuildUpManagersUsingPropertyInjection(List<object> managerInstances)
{
_container.Configure(x => x.SetAllProperties(c =>
{
// configure the property injection for all managers
c.Matching(prop => typeof(IM).IsAssignableFrom(prop.PropertyType));
// client logic is also used by managers
c.TypeMatches(type => type.Equals(typeof(ILogic)));
}));
_container.BuildUp(_container.GetInstance<ILogic>());
managerInstances.ForEach(x => _container.BuildUp(x));
}
结果
StructureMap Exception Code: 295 Bidirectional Dependency Problem detected with RequestedType: StructureMapTests.IMA, Name: 237b6fb1-7dee-4f09-b663-c33bb793afc6, ConcreteType: StructureMapTests.A. The BuildStack is: 1.) RequestedType: StructureMapTests.IMA, Name: 237b6fb1-7dee-4f09-b663-c33bb793afc6, ConcreteType: StructureMapTests.A 2.) RequestedType: StructureMapTests.IMB, Name: ad692281-cd27-493c-8271-3dcf86bacd41, ConcreteType: StructureMapTests.B Exception when building *
分析
第一次测试运行的输出是:
一个
乙
逻辑 --> 一切正常
第二次测试运行的输出是:
一个
乙
构建实例时出现异常
乙
一个
构建实例时出现异常
逻辑
一个
B --> 无限重复
请帮忙:(
编辑 我的目标是 4.0 .Net 框架并使用 StructureMap 版本 2.6.2
最佳答案
我已确认这是当前版本的 StructureMap(截至 2011 年 1 月)中的错误。 setter 注入(inject)策略实际上在容器之间被错误地共享。该问题已报告给 StructureMap mailing list .
关于c# - Structuremap 为每个新的 Container 实例使用静态属性注入(inject)配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4826178/