Stop using dagger
Eslam Ahmad
?? Android & KMP Expert | Kotlin | RxJava | Coroutines | Leader in Driving Innovation
Dagger is a dependency injection library, used to create testable code.
But you payoff time and headache to make your code testable, due to high complexity of dagger implementation.
Starting from adding gradle dependency, through creating components, modules and bla bla (a lot of hassles).
Arnaud Giuliani decide to help us and create a new DI framework make our coding experience more funny without any hassles.
Koin
New way for DI help us to create dependency tree in only 5 min (not egyption 5 min :D), create your classes with constructor inject or with field inject as you like, then create your module That's it!
Let's take an example (ViewModel Injection).
- Add gradle dependency
implementation "org.koin:koin-android-architecture:0.9.3"
- Create Viewmodel take repository in constructor method
// Injected by constructor
class MyViewModel(val repo : MyRepository) : ViewModel(){
}
2. get view model in activity or fragment
val viewModel = getViewModel<MyViewModel>()
or even lazy property
val viewModel by viewModel<MyViewModel>()
or share view model
val viewModel:MyViewModel = sharedViewModel()
3. Create module
val myModule : Module = module {
viewModel { MyViewModel(get()) }
single { MyRepository() }
}
4. start koin in application
class MyApplication : Application() {
override fun onCreate(){
super.onCreate()
// start Koin!
startKoin(this, listOf(myModule))
}
}
That's it!
Without any hassles, in only 5 min :D
I will write articles to cover all koin features and some workarounds to use it in legacy java code.
Stay tuned
Android developer
6 年????????? ?? ?? kotlin ?