0

I have a quick question. Is there any kind of dependencies which shouldn't be injected by DI? Yesterday I saw in my teammate pull request injection of class which have only static methods (typical bag of functional methods). And I've started wondering if it should be injected or not. That class doesn't interfere with any external services. I can't find any clear answer.

Any suggestions?

3
  • Sounds like you already know the answer. Maybe provide an example of the class, it's registration in DI, and how it was resolved/used. Just to be sure. Jun 3, 2020 at 9:18
  • It's a simple class, call it for example UserService. We inject into constructor UserRepository class. And my teammate needed some method to generate random strings, which is placed in e.g. RandomGenerator class. So he injected it as well. But every method in RandomGenerator class is static and works functional way – receives some input, and return some output, without any side effects. I don't need to mock that kind of dependency in my unit tests. But from the other side – calling a static method from another class inside my UserService class hides dependency, doesn't it? Jun 3, 2020 at 9:35
  • And writing "DI" I don't mean Dependency Injection Container. Just a simple DI pattern. Jun 3, 2020 at 9:38

2 Answers 2

2

Dependency injection is delegating the responsibility of creating objects to others, if no object should be created (like utils), or it's not part of your API (like loggers), there is no need for DI. But i suggest to rethink it with TDD approach, if you were writing the test first, that dependency is a pain in the neck? if yes, then you should inject it.

2
  • 1
    Nice explanation. Could you please elaborate your point on TDD approach ?
    – James
    Jun 3, 2020 at 17:26
  • TDD stands for Test Driven Development but you can change it to Test Driven Design because when you write your test first you'r actually writing testable code.In testable code you have to be able to inject mocks instead of dependencies otherwise you have to test the whole system.
    – MSH
    Jun 4, 2020 at 18:26
1

Prefer to inject abstractions, simply because injecting concrete dependencies doesn't add value. Injecting a concrete dependency doesn't invert flow of control, doesn't decouple modules, and doesn't facilitate polymorphism.

Of course the Dependency Inversion Principle advises not to depend on concretions at all, and there is a debate of whether or not utility classes are evil; but ignoring that topic for a moment, I think there are some reasonable answers here and here.

2
  • Why it doesn't decouple modules? I don't say injecting concrete is the best solution, but it's still better than the creation of an object inside the class. At least for unit testing porpuses. Anyway – thank for the valuable answer and for the links, I'll check them! Jun 3, 2020 at 19:46
  • Whether you inject or instantiate a concrete dependency, you are coupled to the same module; you have the same import statement.
    – jaco0646
    Jun 3, 2020 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.