jasfans.blogg.se

Dagger kotlin
Dagger kotlin










dagger kotlin dagger kotlin

To initialize Koin in the project we will update the onCreate() of the application like, override fun onCreate() Here, by viewModel() creates the instance for the ViewModel and it will also resolve the dependency required by it. To pass the instance of ViewModel we will create a variable called GalleryViewModel like, private val viewModel : GalleryViewModel by viewModel() Now, we need to create the instance of ViewModel in the GalleryFragment so the values passed in the constructor of the ViewModel are passed to it. Let us start now by injecting the dependencies. Now, we are done providing the dependencies. Here, you can see we are not using single, and in place of that we are using viewModel and inside it, we resolve the dependencies which we are providing from viewModelModule. The aim is to reduce the boilerplate code required for Dagger and make dependency injection less painful. Koin is written in pure Kotlin using functional resolution only: no proxy, no code generation, no reflection!Īnd what about Hilt? Hilt is the new dependency injection framework built on top of Dagger. With less learning curve and boilerplate code, it seems better than Dagger2. So I did some digging and found this cool, lightweight library called Koin(with multi-platform support). We can’t use Dagger2 in Kotlin multi-platform projects. Meaning the business logic can be written in a single module that can be used by all the supported platforms. If you are an Android developer, chances are you might have used Dagger, which, undoubtedly, is a great library to implement DI.īut android development is evolving rapidly with Kotlin, we can write platform-agnostic code in a common module. It’s unnecessarily complicated, there are so many (confusing) ways of configuring it and it’s verbose. You can’t talk about dependency injection in Android without mentioning Dagger, right? Like every other Android developer, I’m a big sufferer of Dagger 2.

dagger kotlin

Implementing DI provides you with the following advantages:












Dagger kotlin