Introduction
In the previous chapter, we looked at how to structure code into different components, including ViewModels, repositories, API components, and persistence components. One of the difficulties that always emerged was the dependencies between all of these components, especially when it came to how we approached the unit tests for them.
We have constantly used the Application
class to create instances of these components and pass them in the constructors of the components one layer above (we created the API and Room instances, then the Repository instances, and so on). What we were doing was a simplistic version of dependency injection.
Dependency injection (DI) is a software technique in which one object (application) supplies the dependencies of another object (repositories, ViewModels
). The reason for this is to increase the reusability and testability of the code and to shift the responsibility for creating instances from our components to the Application
class...