Stop using dagger

Stop using dagger

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).

  1. Add gradle dependency
implementation "org.koin:koin-android-architecture:0.9.3"
  1. 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


Walaa Abuhasna

Android developer

6 年

????????? ?? ?? kotlin ?

要查看或添加评论,请登录

Eslam Ahmad的更多文章

  • How KMM can enhance business analytics numbers

    How KMM can enhance business analytics numbers

    Please Feel free to add a comment to correct me, recommend an edit, and if you do like it, clap and share ????…

  • Unit Tests - Basics - Part 1

    Unit Tests - Basics - Part 1

    In this series of articles i need to take chance to share my knowledge in unit testing with you, also it will great if…

    11 条评论
  • Koin - Light, simple Kotlin DI Framework

    Koin - Light, simple Kotlin DI Framework

    As i mentioned in previous article, i will write articles to try to illustrate how to use koin in your code, to avoid…

    1 条评论
  • How to use Rxjava with Dagger2 and Retrofit and OkHttp and Gson

    How to use Rxjava with Dagger2 and Retrofit and OkHttp and Gson

    In this article we will see how to create a complete project using rxjava, dagger2, retrofit, okhttp and Gson Click…

    2 条评论
  • Retrofit and RxJava

    Retrofit and RxJava

    What is Retrofit? Simply if you need to send or receive data from server you need to serialize data to be able to…

    8 条评论

社区洞察

其他会员也浏览了