Dependency Dejection

Engineers like solving problems, hence software engineers like stacking them.

Object A uses code B.
Say then, for testing purpose, you have to use B' instead. So you pass the code to use at A's construction (dependency injection).
A is now harder to construct, so you make a factory. You want to make for all the hours passed studying 'Design Patterns'. All of this becomes complex, so you use a dependency framework.
Later on, you struggle understand what the hell is going on. Hopefully a good salary mitigates the pain.

Alternative: if A needs the result of code B, let the client of A invoke code B et pass the value to A as an input.
If the client is a test, it will invoke code B' instead.

Alternative 2: pass the code B at use time rather than construction. I.e. a function as a parameter you pass to A's method of interest.

tl;dr: Instead of dependency injection, what about no dependencies at all?

Nota Bene: As any tool, dependency injection has its utility in specific cases.

Orgams example: rather than passing get_input (actual routine or mock) to field_editor, do get_input ourself, and pass the value.

Comments