12

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?
wonea
  • 4,783
  • 17
  • 86
  • 139
  • Though in general a redesign should be done, here is a reference: http://stackoverflow.com/questions/1316518/how-did-microsoft-create-assemblies-that-have-circular-references/1316558#1316558 – Dykam Sep 20 '09 at 13:22

4 Answers4

34

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/

Andrei Vajna II
  • 4,642
  • 5
  • 35
  • 38
10
  1. Yes, you make it even harder to detect them by using an extra layer of abstraction.
  2. You absolutely do not solve the circular dependency but hide it by adding an extra layer of abstraction, using late bounding, or/and loosely coupling it.

The same answer returns in the following posts (which I will add for references) which is create a 3rd class on which both depend. This translates to: you are violating the Single Responsibility Principle. By moving (extracting) the responsibility both classes depend on in a separate class you'll remove the circular dependency.

FYI the Single Responsibility Pattern on Wikipedia

StackOverflow discussions by others:

My answer on StackOverflow with an example of extracting the responsibility in a seperate class.

Community
  • 1
  • 1
Cohen
  • 2,720
  • 26
  • 24
3

DI is not for circular dependency resolution but rather for facilitating the creation of nicely decoupled components and thus more testable ones.

Juri
  • 32,424
  • 20
  • 102
  • 136
2

I'll toss in my $0.02 here since I found this helpful post by searching for "removing cyclical dependencies"

Yes. You can use DI to resolve circular dependencies. The first step to fixing any issue is finding it. Ninject complained about my circular dependency and threw a helpful exception at bootstrapping. Ninject found it for me and is forcing me to fix it. I could cheat and use property injection or method injection but that breaks my protection of class invariants (which is I think what you are complaining about).

So a vote for ctor injection via IoC because it will spot circular dependencies for you. It is then up to you to refactor and remove the architectural bug.

dFlat
  • 819
  • 7
  • 19
  • Very good post regarding this: https://dotnetcoretutorials.com/2020/09/14/dealing-with-circular-dependency-injection-references/ – Dmitry Bogatykh Aug 08 '21 at 20:11