Solution 1 :
There is no problem in simply passing references of classes in the constructor of the dependent class.
Motive behind using dependency injection is to simplify the complexity of code as dependency injection provides the re-usability of classes and decoupling of dependencies making code easy to refactor and test.
Problem :
I am learning usage of DI in android and more often I have came across how we are required to use dependency Injection. Before that while developing apps I used to simply pass references of classes in the constructor of the dependent class. Why can’t we keep doing that rather than using constructor Injection? Thank you.
Comments
Comment posted by developer.android.com/training/…
Dependency injection provides your app : Reusability of classes and decoupling of dependencies check out more on
Comment posted by Steven
Can you show some examples that demonstrate what the difference is between “simply pass references of classes in the constructor of the dependent class” and what you consider to be “constructor injection”?
Comment posted by charmcaster
While giving interview at Uber the interviewer asked me this question why I am using @Inject constructor , for example in MVVM while passing reference of Repository class into viewmodel constructor. He asked me why I am using dependency Injection if I can simply pass the reference.