What is DI?
In simple terms, DI represents the concept of providing the instances of the dependencies that a class needs, instead of having it construct them itself. But, what are dependencies?
Dependencies are other classes that a certain class depends on. For example, an ExampleViewModel
class could contain a repository
variable of type Repository
:
class ExampleViewModel { private val repository: Repository = Repository() fun doSomething() { repository.use() } }
That's why ExampleViewModel
depends on Repository
, or Repository
is a dependency for ExampleViewModel
. Most of the time, classes have many more dependencies, but we'll stick with only one for the sake of simplicity. In this case, the ExampleViewModel
provides its own dependencies so it's very easy to create an instance of it:
fun main() { val vm = ExampleViewModel...