Resolving circular dependencies with dependency injection

BostonCoder picture BostonCoder · Sep 20, 2009 · Viewed 15.4k times · Source

I've seen several articles on various websites that propose resolving circular dependencies between .NET assemblies by using dependency injection. This may resolve the build errors but it's not really resolving the circular dependency, is it? To me, there seems to still be a logical error in the architecture. Am I crazy or do others agree?

  1. This is a less than stellar use of DI?
  2. Not the appropriate way to solve circular dependency issues?

Answer

Andrei Vajna II picture Andrei Vajna II · Sep 20, 2009

If you have circular dependencies between two objects, it means you need a third object, on which the two objects will depend on, so they won't depend on each other. Here is an article that is the exact solution to your problem:

http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/