Dependency injection
Dependency injection is a way for us to manage and provide dependencies that a class needs to do its work without the class having to create the dependencies itself. In this book, we will be using Koin (https://insert-koin.io/) as our dependency injection library.
Our PetsViewModel
class creates the PetsRepository
class by itself. This is a suitable candidate for dependency injection. We will be refactoring this to use dependency injection. Let’s start by adding the Koin dependency to our app. Open the build.gradle
file for the app module and add the following dependency:
implementation 'io.insert-koin:koin-core:3.4.3' implementation 'io.insert-koin:koin-android:3.4.3' implementation 'io.insert-koin:koin-androidx-compose:3.4.6'
We are adding the Koin core
, android,
and compose
dependencies as well, which will be used in our project to provide the dependencies.
After adding this to our project and syncing the project...