vendredi 11 octobre 2019

Implement chain of responsibility in StructureMap

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

interface IDoStuff
{
  IDoStuff Next {get; set}

  void Configure();
}

class StepOne
{
  public IDoStuff Next {get; set}

  public StepOne(IDoStuff next)
  {
    Next = next;
  }

  public void Configure()
  {
    // Do configuration relevant to StepOne

    if (Next != null)
      Next.Configure()
  }
}

class StepTwo
{
  public IDoStuff Next {get; set}

  public StepTwo(IDoStuff next)
  {
    Next = next;
  }

  public void Configure()
  {
    // Do configuration relevant to StepTwo

    if (Next != null)
      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?

Aucun commentaire:

Enregistrer un commentaire