4 ms·
It's really just a criticism of dependency injection /frameworks/. Manually wiring dependencies by calling constructors is still dependency injection. This als
by benjiweber 10y ago
It's really just a criticism of dependency injection /frameworks/.
Manually wiring dependencies by calling constructors is still dependency injection. This also has the upside that the compiler will check you have satisfied all dependencies, and you can more easily trace execution.
Frameworks like Guice/Spring are often used to hide lots of wiring boilerplate but come at a price of losing typesafety, adding more magic, and hiding some code smells. If your wiring is getting complex perhaps you need more modularity.
- CydeWeys 10y agoYour criticisms seem to only apply to runtime dependency injection frameworks. That's why I like Dagger, and recommended it elsewhere in this thread; it's compile-time dependency injection. So you do get type safety, the compiler does check that you have satisfied all dependencies, etc. Check it out if you haven't used it before.
- benjiweber 10y agoYou're right, most of the downsides are specific to runtime wiring. I haven't used dagger. It does compile time magic doesn't it?
- CydeWeys 10y agoYes. If you do something wrong in your wire-up then the code won't even compile. Definitely look into it. Like you, I have a healthy distrust of DI frameworks because of the many errors that get pushed to runtime, but Dagger's not bad.
- beder 10y ago(Disclosure: I work on Dagger in my 20% at Google.) What's more, Dagger gives you (compile-time) insights into the graph of dependencies. For example, we've hooked up our internal code-search tool to give you Dagger cross-references, so if you (e.g.) click on a parameter to an @Inject constructor, you'll see the place that provides that object. Also, if you click on the @Component, you'll see a visualization of the entire graph. We're working on surfacing these hooks in the open source repo, which would allow IDEs like Eclipse to gain this functionality as well.