Skip to main content
1 of 3
Aviv Cohn
  • 21.6k
  • 35
  • 127
  • 186

Dependency injection: should I use a framework?

I have recently worked on a Python project where we did dependency injection heavily (because we must in order for the app to be testable), but we didn't use any framework. At times it was a little tedious to wire up all the dependencies manually, but overall it worked great.

When an object had to be created in multiple places, we simply had a function (sometimes a class method of that object) to create a production instance. This function was called whenever we needed that object.

In the tests, we did the same thing: if we needed to create a 'test-version' of an object (i.e., a real instance backed by mock objects), and this happened more than once, we had a function to create this 'test-version' of a class, to be used in tests.

As I said, generally this worked fine.

I am now entering a new Java project, and we are deciding whether to use a DI framework, or do DI manually (like in the previous project).

Please explain how working with a DI framework will be different, and in what manners it is better or worse than manual 'vanilla' dependency injection.

Aviv Cohn
  • 21.6k
  • 35
  • 127
  • 186