The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

jjenkov (13) [Avatar] Offline
Chapter 3 discusses cyclic dependencies among other things.
In particular, the text shows how you can get around instantiating
objects which have cyclic dependencies in their constructors.

In my opinion, having cyclic dependencies in your constructors is
a design flaw. Any maintenance developer looking those two classes
is going to be rather puzzled about how to instantiate them, and
what their relationship is. If you have components with cyclic
dependencies I consider it better pratice to

1) Create an interface for the components with only the necessary methods exposed.

2) Have the extra methods (setter or setters) in the implementation classes that allow you to
wire up the cyclic dependency.

3) Delegate object creation to a factory. That way the wiring up is hidden from the public
and so are the methods for creating this wiring. They should not be exposed in
the components interfaces, but only be available to the factory.

Just my 2 cents on that topic. Otherwise another great chapter in what is to become
THE book on DI in my opinion smilie
dhanji.prasanna (37) [Avatar] Offline
Re: Chapter 3 - Cyclic Dependencies in Constructors
Sure, I believe I mention as much--that it should only be applied in rare cases. Sometimes you cant help it as you are refactoring legacy code to use DI.

Thanks for the kind words.
jjenkov (13) [Avatar] Offline
Re: Chapter 3 - Cyclic Dependencies in Constructors
Hmm... you do it seems, having looked at the text once more.