StructureMap problems with bidirectional/circular dependencies
- by leozilla
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