【发布时间】:2019-10-14 17:08:44
【问题描述】:
假设我在以下类中实现了责任链设计模式;
interface IDoStuff
{
IDoStuff Next {get;}
void Configure();
}
class StepOne
{
public IDoStuff Next {get; set}
public StepOne(IDoStuff next)
{
Next = next;
}
public void Configure()
{
// Do configuration relevant to StepOne
// Call the next configure step in the chain
Next?.Configure()
}
}
class StepTwo
{
public IDoStuff Next {get; set}
public StepTwo(IDoStuff next)
{
Next = next;
}
public void Configure()
{
// Do configuration relevant to StepTwo
// Call the next configure step in the chain
Next?.Configure()
}
}
我尝试这样配置链
class MyRegistry : Registry
{
public MyRegistry()
{
For<IDoStuff>().Use<StepOne>();
For<StepOne>().Use(c => new StepOne(c.GetInstance<StepTwo>()));
For<StepTwo>().Use(c => new StepTwo(null));
}
}
但是我从 StructureMap 中得到了这个错误;
检测到双向依赖关系! 检查下面的 StructureMap 堆栈跟踪: 1.) IDoStuff (StepOne) 2.) new StepOne(IDoStuff 的默认值) 3.) StepOne 4.) IDoStuff 实例(StepOne) 5.) Container.GetInstance()
注册链的正确方法是什么?
【问题讨论】:
标签: design-patterns structuremap chain-of-responsibility