Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GDG DevFest in Taipei 2019: Enjoy DI life with ...

Avatar for Aaron Aaron
December 01, 2019

GDG DevFest in Taipei 2019: Enjoy DI life with Koin

When mentioning about DI framework on Android, in addition to Dagger2, there is actually another alternative, Koin. Koin is a lightweight dependency injection framework written in pure Kotlin. As a DSL, Koin provides a pragmatic API that significantly simplifies the injection process.

This session will talk about how easy to set up the dependencies in Koin and how easy to inject instances. Besides, We will demonstrate how to integrate Koin with Model-View-Presenter (MVP) and Model-View-ViewModel (MVVM). Lastly, we will talk about testing with Koin.

Avatar for Aaron

Aaron

December 01, 2019

Other Decks in Technology

Transcript

  1. Agenda • Dagger2 • Koin Basics • Model-View-Presenter (MVP) with

    Koin • Model-View-ViewModel (MVVM) with Koin • Inject Anywhere • Koin Test
  2. Agenda • Dagger2 • Koin Basics • Model-View-Presenter (MVP) with

    Koin • Model-View-ViewModel (MVVM) with Koin • Inject Anywhere • Koin Test
  3. Dagger2 @Component @Module @Provides @Singleton @Scope @Qualifier @Named @Inject @SubComponent

    @ContributesAndroidInjector AndroidSupportInjectionModule AndroidInjector @Binds @Component.Builder @Component.Factory @IntoMap @BindsInstance @MapKey @Reusable MembersInjector HasAndroidInjector DaggerAppCompatActivity @BindsOptionalOf DaggerApplication
  4. Agenda • Dagger2 • Koin Basics • Model-View-Presenter (MVP) with

    Koin • Model-View-ViewModel (MVVM) with Koin • Inject Anywhere • Koin Test
  5. What is Koin? A pragmatic lightweight dependency injection framework for

    Kotlin developers. Written in pure Kotlin using functional resolution only: no proxy, no code generation, no reflection! Koin is a DSL, a lightweight container and a pragmatic API.
  6. • Activity • Fragment • Service • ContentProvider • Broadcast

    Receivers • ... inject() and get() are extension functions of ComponentCallbacks interface Where are inject() and get() from?
  7. Agenda • Dagger2 • Koin Basics • Model-View-Presenter (MVP) with

    Koin • Model-View-ViewModel (MVVM) with Koin • Inject Anywhere • Koin Test
  8. • FragmentActivity • AppCompatActivity • Fragment • DialogFragment • ...

    Where is currentScope from? currentScope is an extension property of LifecycleOwner
  9. Agenda • Dagger2 • Koin Basics • Model-View-Presenter (MVP) with

    Koin • Model-View-ViewModel (MVVM) with Koin • Inject Anywhere • Koin Test
  10. Where are viewModel() and sharedViewModel() from? viewModel() and getViewModel() are

    extension functions of LifecycleOwner sharedViewModel() and getSharedViewModel() are extension functions of Fragment
  11. Agenda • Dagger2 • Koin Basics • Model-View-Presenter (MVP) with

    Koin • Model-View-ViewModel (MVVM) with Koin • Inject Anywhere • Koin Test
  12. Inject view model declared in viewModel() anywhere? No, because ViewModel

    is tied to FragmentActivity and Fragment. The way of getting a ViewModel, • ViewModelProviders.of(FragmentActivity).get(...) • ViewModelProviders.of(Fragment).get(...)
  13. Agenda • Dagger2 • Koin Basics • Model-View-Presenter (MVP) with

    Koin • Model-View-ViewModel (MVVM) with Koin • Inject Anywhere • Koin Test
  14. • single(), factory() ◦ inject(), get() from CallbackComponents or KoinComponent

    • scope(), scoped() ◦ currentScope from LifecycleOwner ◦ koin.createScope(...) from KoinComponent • viewModel() ◦ viewModel() from FragmentActivity, sharedViewModel() from Fragment Koin injection from different definitions Summary
  15. Summary Koin test • startKoin() with predefined modules and AutoCloseKoinTest

    • KoinApplication.checkModules() • KoinTest.declareMock() for non-scope definitions • Scope.declareMock() for scope definitions