Reputation: 616
If you search for how to resolve circular dependencies, the answer is almost always "use interfaces". I know how this technique can be used to make a circular dependency work, but I do not understand how that is solving a circular dependency.
Say I have 2 classes View and Presenter which reference each other. Now I apply the "solution" and create the interfaces IView and IPresenter. View does not reference Presenter anymore, but IPresenter; Presenter references IView instead of View.
The circle has gotten bigger, but it is still there. View and Presenter still depend on each other, just not directly. However, every answer I have seen is absolutely sure that the circular dependency is now resolved. What is my misunderstanding here?
Upvotes: 6
Views: 4182
Reputation: 616
These circular dependencies are not being solved.
The standard answer in these situations is to assign the dependency after construction. This solves the problem that these circular dependencies create, but not the circular dependencies themselves.
It's probably mostly a language issue where people just say "I solved the circular dependency between X and Y" instead of "I solved the issue we had because of the circular dependency between X and Y".
Upvotes: 5
Reputation: 38714
Circular dependencies can be resolved using DI but only using "delayed" or "property" injection, not "constructor" injection. You would need to construct the objects first, then set properties on them to inject the dependencies. A DI container that supports property injection could abstract this detail away.
Upvotes: 3