Re:Dependency injection on Android with dagger-android and K...
by aristidb on Wednesday April 18, 2018 @03:00AM
(#16865265)
I've never quite understood the appeal of Dependency Injection. What's wrong with just using constructors?
[ Parent
| on HN ]
Re:Dependency injection on Android with dagger-android and K...
by V-2 on Wednesday April 18, 2018 @04:19AM
(#16865658)
For simple scenarios DI is obviously an overkill, but as soon as you're dealing with nested dependencies, you'd end up with either:
* monstrously big constructors (for carrying transitive dependencies)
* lots of @VisibleForTesting code to handle manually injecting various dependencies only for the sake of testing (poor man's DI and generally bad practice)
* a lot of factories (service locator or poor man's DI, essentially)
* code that's hard to unit test due to dependencies being hardcoded.
In other words, you'll either reinvent DI poorly, or give up on testability.
Re:Dependency injection on Android with dagger-android and K...
by AndrewDucker on Wednesday April 18, 2018 @03:22AM
(#16865377)
Constructors in the middle of your code make it harder to substitute mock objects for testing purposes.
Re:Dependency injection on Android with dagger-android and K...
by romanovcode on Wednesday April 18, 2018 @08:07AM
(#16866630)
The problem is not the testing. The problem is that if you have classes that depend on other classes that depend on other classes that depend on specific configuration.
Then you have a mess without DI.