0

Imagine I implement the chain of responsibility design pattern in the following classes;

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()
  }
}

I tried to configure the chain like this

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));
  }
}

But instead I get this error from StructureMap;

Bi-directional dependency relationship detected! Check the StructureMap stacktrace below: 1.) IDoStuff (StepOne) 2.) new StepOne(Default of IDoStuff) 3.) StepOne 4.) Instance of IDoStuff (StepOne) 5.) Container.GetInstance()

What is the correct way of registering the chain?

Steztric
  • 2,832
  • 2
  • 24
  • 43

1 Answers1

0

I figured it out. I do not need the first line in the registry. It should be like this;

class MyRegistry : Registry
{
  public MyRegistry()
  {
    For<IDoStuff>().Use(c => new StepOne(c.GetInstance<StepTwo>()));
    For<StepTwo>().Use(c => new StepTwo(null));
  }
}

Then the chain will be constructed so that if you do container.GetInstance<IDoStuff>() it will give you a chain that goes StepOne => StepTwo => null.

Steztric
  • 2,832
  • 2
  • 24
  • 43