views:

122

answers:

1

I am currently integrating StructureMap within our business layer but have problems because of bidirectional dependencies.

The layer contains multiple manager where each manager can call methods on each other, there are no restrictions or rules for communication. This also includes possible circular dependencies like in the example below. I know the design itself is questionable but currently we just want StructureMap to work and will focus on further refactoring in the future.

Every manager implements the IManager interface

internal interface IManager
{
  bool IsStarted { get; }

  void Start();
  void Stop();
}

And does also have his own specific interface.

internal interface IManagerA : IManager
{
  void ALogic();
}

internal interface IManagerB : IManager
{
  void BLogic();
}

Here are to dummy manager implementations.

internal class ManagerA : IManagerA
{
  public IManagerB ManagerB { get; set; }

  public void ALogic() { }

  public bool IsStarted { get; private set; }
  public void Start() { }
  public void Stop() { }
}

internal class ManagerB : IManagerB
{
  public IManagerA ManagerA { get; set; }

  public void BLogic() { }

  public bool IsStarted { get; private set; }
  public void Start() { }
  public void Stop() { }
}

Here is the StructureMap configuration i use atm.

I am still not sure how i should register the managers so currently i use a manual registration. Maybee someone could help me with this too.

For<IManagerA>().Singleton().Use<ManagerA>();
For<IManagerB>().Singleton().Use<ManagerB>();

SetAllProperties(convention =>
{
  // configure the property injection for all managers
  convention.Matching(prop => typeof(IManager).IsAssignableFrom(prop.PropertyType));
});

After all i cannot create IManagerA because StructureMap complians about the circular dependency between ManagerA and ManagerB. Is there an easy and clean solution to solve this problem but keep to current design?

br David

+1  A: 

Patient: "Doc, it hurts when I poke my finger in my eye"

Doctor: "Stop poking your finger in your eye."

No, there will be no "easy", nor "clean" solution that keeps the current design. When intuition, and your tools, tell you a design isn't working, its probably a good idea to listen.

If something needs an IManagerA, and an IManagerB, then it should take them both as dependencies. A dependency injection tool, like StructureMap, makes these types of composition scenarios easy - don't fight it.

Joshua Flanagan