I’m a constructor-based dependency injection kinda guy but with everyone always talking about setter-based dependency injection I started to question my approach. When Dave Thomas reminded me about class invariants I knew that my constructor-based approach was the right one.
…a class does not allow use of all possible values for the state of the object, only those that are well-defined by the semantics of the intended use…
The main purpose of a constructor is to establish the invariant of the class, failing if the invariant isn’t valid.
You know that you should use constructor-based dependency injection when the dependency answers the question is this a class invariant.