📎 Webclip
Handling Dependency Injection in Inherited Classes
The post explains that dependency injection works well for testability, maintainability, and reusability, but inheritance makes it awkward when a base class gains more shared dependencies. Every concrete handler then needs its constructor updated to pass the new arguments through to the base class.
Reading notes#
- Dependency injection is presented as a useful pattern because it improves testability, maintainability, and reusability.
- In inherited classes, adding a new dependency to the base class forces changes in every concrete subclass constructor.
- Large constructor signatures make the class intent harder to read.
- The proposed fix is to create a dependency aggregate that groups the shared dependencies.
- The base handler receives the aggregate instead of individual dependencies.
- Concrete handlers then depend on the aggregate, so adding a new shared dependency only requires changes in the aggregate class and the base class.
