Example:
class MyClass
{
Composition m_Composition;
void MyClass()
{
m_Composition = new Composition( this );
}
}
I am interested in using depenency-injection here. So I will have to refactor the constructor to something like:
void MyClass( Composition composition )
{
m_Composition = composition;
}
However I get a problem now, since the Composition
-object relies on the object of type MyClass
which is just created.
Can a dependency container resolve this? Is it supposed to do so?
Or is it just bad design from the beginning on?
A circular dependency isn’t hard to understand, it basically just means we told Guice how to construct an object to satisfy a certain dependency, and in order to do so, it ends up needing that same dependency. A simplified example might be: When a dependency of type Chicken is needed, get an Egg and call Egg::hatch () on it to get the Chicken.
A circular dependency was detected for the service of type 'UserService'. UserService -> System.Lazy -> PhoneService -> UserService So then, people would just inject some sort of ServiceLocator. For example : This does compile and actually run, but again is simply a band aid to the problem.
Another way to break the cycle is injecting a dependency using @Autowired on one of the beans, and then use a method annotated with @PostConstruct to set the other dependency. Our beans could have the following code: @Component public class CircularDependencyB { private CircularDependencyA circA; private String message = "Hi!"
Dependency Injection is a way of implementing the dependency inversion principle (DIP), which essentially just means that instead of having your class instantiate objects that it depends on (e.g., HTTP clients, loggers, etc), these objects should be provided to your class.
No, a DI Container will not solve a circular dependency - in fact, it will protest against it by throwing exceptions when you try to resolve the dependencies.
In many of the DI Containers you can provide advanced configuration that allows you to overcome this issue, but by themselves they can't resolve circular dependencies. How could they?
As a rule of thumb, a circular depedency is a design smell. If you can, consider an alternative design where you get rid of the circular dependency - this will also give you less coupling. Some possible redesign alternatives:
However, I purposedly chose the word smell over anti-pattern, as there are corner cases (particularly when you deal with externally defined APIs) where circular dependencies cannot be avoided.
In such cases, you need to decide where to loosen the dependency creation slightly. Once you know that, injection of an Abstract Factory may be helpful to defer one of the creations until the other parts of the circle have been created.
This other answer is the best, available example of which I'm currently aware, but if I may be so bold, my upcoming book will also contain a section that addresses this very issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With