From the course: Android Compose with Kotlin

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Dependency injection with Koin

Dependency injection with Koin - Kotlin Tutorial

From the course: Android Compose with Kotlin

Dependency injection with Koin

- [Instructor] Koin is a Kotlin dependency injection framework. I'm using it in our sample app. Dependency injection is recommended on Android as it improves testability and maintainability. Let me walk you through the setup, but first, a mini review of dependency injection. Starting on line number six, we have a ViewModel class, and it creates an instance of the DefaultSampleRepository. Then it makes a call to its doSomething method on line number 10. The Repository class is a dependency of the ViewModel. Now, let's change this to use dependency injection. So instead of this, we'll copy the DefaultSampleRepository value, and we're going to put that inside of the sample ViewModel constructor. Then we'll remove this and just like that, we're using dependency injection. So if we create an instance of the ViewModel, we would now do val viewModel, and we'll set that equal to the SampleViewModel class, passing in an instance of the DefaultSampleRepository. It's pretty straightforward…

Contents