By now, you are probably thinking, there are times when using DI is not the best option, but how do I know when? For this, I would like to offer you another self-survey.
When you are unsure how to proceed, or before you embark on a potentially big refactor, first take a quick run through my DI Survey:
- Is the dependency an environmental concern (such as logging)?
Environmental dependencies are necessary but have a tendency to pollute the UX of the function, particularly a constructor. Injecting them is appropriate, but you should prefer a less obtrusive DI method such as JIT injection or config injection. - Are there tests in place to protect us during refactoring?
When applying DI to existing code that has low test coverage, adding some monkey patching will be the smallest change you can make and therefore the one that poses the least risk. Once tests...