c # – Inject a dependency that is a dependency tree

container.DependencyA.DependencyAA.Method (…)

This sounds like a violation of the Demeter law. The problem has nothing to do with addictions.

The problem is to reach the objects of the past and to violate their abstraction. This is a problem because if even a client writes code like: container.DependencyA.DependencyAA.Method () so this whole chain is engraved in stone. You are not free to build it otherwise. If the customer has referred to is like depAA.Method () we would then have some flexibility.

Now, that does not mean you never want to see chains of points. It's good when you have been promised that the path will always be true. For example, Java8 streams allow you to link wonderfully long strings. This is good because these strings are expected. You were promised that they would not change.

The problem is when the authors of the clients are interested in the code base and sew together the working paths. Now, this carefully decoupled code base is tied into a mudball because someone was just trying to get what he needed instead of just asking for it to be delivered to him.

The idea is that it is better that each object has some friends with whom it knows how to speak. These friends have friends with whom they know how to talk. If you talk to your friends soon, you must know how to talk to everyone. It's bad if we want to one day be able to implement a change because it has an impact on who knows what has changed.

Use the injection of facades, abstractions and dependencies to prevent customers from knowing how your object graph is wired and you keep the freedom to connect them as you see fit.

If you need to allow channels, be sure to decouple the chain from the implementation below. These are called iDSL (Internal Domain Specific Languages). They are very powerful but require a lot of work to put in place. Authorized channels form the mini-language. Make sure you can exchange the implementation behind the chain.