When to Use Dependency Injection
Join the DZone community and get the full member experience.Join For Free
a great question from a reader...
the only thing that does not fully convince me in your articles is usage of guice. i’m currently unable to see clearly its advantages over plain factories, crafted by hand. do you recommend using of guice in every single case? i strongly suspect, there are cases, where hand-crafted factories make a better fit than guice. could you comment on that (possibly at your website)?
i think this is multi-part question:
- should i be using dependency-injection?
- should i be using manual dependency-injection or automatic dependency-injection framework?
- which automatic dependency-injection framework should i use?
should i be using dependency-injection?
dependency injection is simply a good idea and it helps with: testability; maintenance; and bringing new people up to speed on new code-base. dependency-injection helps you with writing good software whether it is a small project of one or large project with a team of collaborators.
should i be using manual dependency-injection or automatic dependency-injection framework?
whether or not to use a framework for dependency injection depends a lot on your preferences and the size of your project. you don’t get any additional magical powers by using a framework. i personally like to use frameworks on medium to large projects but stick to manual di with small projects. here are some arguments both ways to help you make a decision.
in favor of manual di:
- simple: nothing to learn, no dependencies.
- no reflection magic: in ide it is easy to find out who calls the constructors.
- even developers who do not understand di can follow and contribute to projects.
in favor of automatic di framework:
- consistency: on a large team a lot can be said in doing things in consistent manner. frameworks help a lot here.
- declarative: the wiring, scopes and rules of instantiation are declarative. this makes it easier to understand how the application is wired together and easier to change.
- less typing: no need to create the factory classes by hand.
- helps with end-to-end tests: for end-to-end tests we often need to replace key components of the application with fake implementations, an automated framework can be of great help.
which automatic dependency-injection framework should i use?
i work for google, i have used guice extensively therefor my default recommendation will be guice. however i am going to attempt to be objective about the differences. keep in mind that i have not actually used the other ones on real projects.
spring was first. as a result it goes far beyond di and has everything and kitchen sink integrated into it which is very impressive. the di part of spring has some differences worth pointing out. unlike guice or pico, spring uses xml files for configuration. both are declarative but guice is compiled and as a result guice can take advantage of compiler type safety and generics, which i think is a great plus for guice.
historically, spring started with setter injection. pico introduced constructor injection. today, all frameworks can do both setter and constructor injection, but the developers using these frameworks still have their preferences. guice and pico strongly prefer constructor injection while spring is in the setter injection camp. i prefer constructor injection but the reasons are better left for another post.
personally, i think all of the three have been around for a while and have proven themselves extensively, so no matter which one you chose you will benefit greatly from your decision. all three frameworks have been heavily influenced by each other and on a macro level are very similar.
(i personally would not chose spring because xml and setter injection puts me off. however i am looking forward to using pico on my next open-source project so that i can become more objective about the differences.)
your milage may vary.
Opinions expressed by DZone contributors are their own.