jeudi 9 janvier 2020

Pass by reference dependency injection

I'm trying to understand dependency injection from the point of view of "Global state is evil". Global state could be avoided by dependency injection as the dependency is then only allowed to be modified in the local scope of a function/class the dependency is injected into. This holds true if the dependency is passed in by value.

But passing the dependency by reference/pointer would still allow other parts/systems to change the state of the dependency injected during its lifetime in the class/function it is injected into.

Is passing by reference an incorrect way of injecting dependency? Should we own the dependency through a unique_ptr as in C++? (whats the alternative in Java?) What is the correct way of injecting dependency and how can we avoid having to reason about gloabl state beyond the lexical scope of the dependency that is injected?

Aucun commentaire:

Enregistrer un commentaire