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

Dependency Injection Koin Vs. Dagger

Su Myat
October 05, 2019

Dependency Injection Koin Vs. Dagger

Dependency Injection is mainly used for the control and management of dependencies. Koin is a simple and powerful dependencies injection library. It’s written with Kotlin. This topic will cover what Koin is, compare with Dagger and how to use it. In order to properly compare the two implementation options, firstly will focus on a project that Implemented first with Dagger and now with Koin.

Su Myat

October 05, 2019
Tweet

More Decks by Su Myat

Other Decks in Programming

Transcript

  1. SA WAD DEE KA Su Myat WTM ambassador Android Team

    Leader at Wave Money @devsumyat
  2. No Injection class A { val b = B() fun

    doSomething() { b.info } }
  3. Types of Dependency Injection : Field class A { @Inject

    val b: B fun doSomething() { b.info } }
  4. Types of Dependency Injection : Field class A { @Inject

    val b: B fun doSomething() { b.info } }
  5. - More reusable - More readable - More extendable -

    Easy replace - Easy switching - Testable Advantage?
  6. - 100% Kotlin - DSL very intuitive - No code

    generation - No reflection - No compile time overhead Koin
  7. Dagger : Dependencies & View Model implementation 'com.google.dagger:dagger:2.11' implementation 'com.google.dagger:dagger-android-support:2.11'

    kapt 'com.google.dagger:dagger-compiler:2.11' kapt ‘com.google.dagger:dagger-android-processor:2.11’ class NewsViewModel @Inject constructor(private val news: News) : ViewModel() { fun getNews(): String { return news.fetchNews() } }
  8. @Singleton class ViewModelFactory @Inject constructor( private val creators: Map<Class<out ViewModel>,

    @JvmSuppressWildcards Provider<ViewModel>> ) : ViewModelProvider.Factory { override fun <T : ViewModel> create(modelClass: Class<T>): T { val creator = creators[modelClass] ?: creators.entries.firstOrNull { modelClass.isAssignableFrom(it.key) }?.value ?: throw IllegalArgumentException("unknown model class $modelClass") try { @Suppress("UNCHECKED_CAST") return creator.get() as T } catch (e: Exception) { throw RuntimeException(e) } } } Dagger : View Model Factory
  9. @Module abstract class ViewModelModule { @Binds @IntoMap @ViewModelKey(BrowseNewsViewModel::class) abstract fun

    bindNewViewModel(newsViewModel: NewsViewModel): ViewModel @Binds abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory } Dagger : View Model Module
  10. @Module open class ApplicationModule { @Provides @PerApplication fun provideNews() =

    News } @Module abstract class ActivityModule { @ContributesAndroidInjector abstract fun mainActivityInjector(): MainActivity } Dagger : Application Module and Activity Module
  11. @Singleton @Component( modules = [ AndroidInjectionModule::class, AndroidSupportInjectionModule::class, ApplicationModule::class, ActivityModule::class, ViewModelModule::class

    ] ) internal interface ApplicationComponent : AndroidInjector<SampleApplication> { @Component.Builder abstract class Builder : AndroidInjector.Builder<SampleApplication>() } Dagger : Application Component
  12. class SampleApplication : DaggerApplication() { override fun applicationInjector(): AndroidInjector<out DaggerApplication>

    = DaggerApplicationComponent.builder().create(this) } Dagger : Inject into Application
  13. class MainActivity : DaggerAppCompatActivity() { @Inject lateinit var viewModelFactory: ViewModelProvider.Factory

    private val viewmodel by lazy { ViewModelProviders.of( this, viewModelFactory )[NewsViewModel::class.java] } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) txt_new.text = viewmodel.getNews() } } Dagger : Inject into Application
  14. Koin : Dependencies & View Model implementation 'org.koin:koin-android:2.0.1' implementation 'org.koin:koin-androidx-scope:2.0.1'

    implementation 'org.koin:koin-androidx-viewmodel:2.0.1' class NewsViewModel(private val news: News) : ViewModel() { fun sendMessage() :String{ return news.fetchNews() } }
  15. class App : Application() { override fun onCreate() { super.onCreate()

    startKoin { androidContext(this@App) modules(module) } } companion object { private val module = module { single { GetNews(get()) } viewModel { BrowseNewsViewModel(get()) } } } } Koin : Application
  16. class BrowseActivity: AppCompatActivity() { val browseNewsViewModel: BrowseNewsViewModel by viewModel() override

    fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_browse) browseNewsViewModel.fetchNews() } } Koin : Class
  17. #1 #2 #3 Let’s take some steps back and learn

    about Inversion of Control - the concept behind DI More exploring the fifth principle of S.O.L.I.D Hope that it makes you wanted to try Koin Takeaways