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

Migrate to Koin

itome
December 13, 2018
300

Migrate to Koin

itome

December 13, 2018
Tweet

Transcript

  1. Koinͱ͸ʁ 7min ɾKotlin੡ͷܰྔDIϥΠϒϥϦ ɾno proxy, no code generation, no reflection!

    ɾReady for ViewModel ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ ɾEasy testing
  2. Koinͱ͸ʁ ɾKotlin੡ͷܰྔDIϥΠϒϥϦ ɾno proxy, no code generation, no reflection! ɾReady

    for ViewModel ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ ɾEasy testing 7min
  3. Koinͱ͸ʁ ɾKotlin੡ͷܰྔDIϥΠϒϥϦ // Add Jcenter to your repositories if needed

    repositories { jcenter() } dependencies { // Koin for Android compile 'org.koin:koin-android:1.0.2' } Daggerͱಉ༷ʹAndroidݶఆϥΠϒϥϦͰ͸ͳ͍ͨΊɺ Spark΍KtorͳͲͷαʔόʔαΠυKotlinͰ΋ར༻Մೳ koin-androidϞδϡʔϧΛbuild.gradleʹهड़͢Δ͜ͱͰAndroidͰར༻Ͱ͖Δ υΩϡϝϯτ͕ॆ࣮͍ͯ͠ΔͷͰɺDIͷ֓೦Λཧղ͍ͯ͠Ε͹͙͢ʹ࢖͍࢝ΊΒΕΔ 7min
  4. Koinͱ͸ʁ ɾKotlin੡ͷܰྔDIϥΠϒϥϦ ɾno proxy, no code generation, no reflection! ɾReady

    for ViewModel ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ ɾEasy testing 7min
  5. ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ val appModule = module { // single instance

    of HelloRepository single<HelloRepository> { HelloRepositoryImpl() } // Simple Presenter Factory factory { MySimplePresenter(get()) } } module಺ʹscopeؔ਺Λهड़ͯ͠ґଘੑΛ௥Ճ͍ͯ͘͠ single: ΞϓϦέʔγϣϯͱಉ͡ϥΠϑαΠΫϧΛ࣋ͭΠϯελϯεΛ ࡞੒͢Δ ɻ factory: ඞཁʹͳΔͨͼʹผͷΠϯελϯεΛ࡞੒͢Δɻ scope: ΧελϜείʔϓΛఆٛͰ͖Δɻ Koinͱ͸ʁ 7min
  6. ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ val appModule = module { // single instance

    of HelloRepository single<HelloRepository> { HelloRepositoryImpl() } // Simple Presenter Factory factory { MySimplePresenter(get()) } } Koinͱ͸ʁ Daggerͱҧ͍ɺґଘάϥϑʹ͋ΔܕΛConstructor Injection͢Δ͚ͩͷ৔߹Ͱ΋ɺ ఆٛͷίʔυΛmoduleʹॻ͔ͳͯ͘͸ͳΒͳ͍ ͢Ͱʹґଘੑ͕ఆٛ͞Ε͍ͯΔΫϥε͸ get()ͰऔಘͰ͖ΔͷͰɺ Constructor Injection͢Δͱ͖͸Ҿ਺ͷ਺͚ͩget()Λॻ͚ͩ͘Ͱ͍͍ 7min
  7. ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ Koinͱ͸ʁ class MainApplication : Application() { override fun

    onCreate() { super.onCreate() // start Koin context startKoin(this, listOf(appModule) } } ApplicationΫϥεͷonCreateͰ startKoin ʹModuleͷϦετΛ౉͢͜ͱͰ ґଘੑάϥϑ͕࡞੒͞ΕΔɻ 7min
  8. Koinͱ͸ʁ ɾKotlin੡ͷܰྔDIϥΠϒϥϦ ɾno proxy, no code generation, no reflection! ɾReady

    for ViewModel ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ ɾEasy testing 7min
  9. Koinͱ͸ʁ ɾno proxy, no code generation, no reflection! Koin͸࣮ࡍͷґଘੑͷ஫ೖΛKotlinͷDelegateΛ࢖࣮ͬͯݱ͍ͯ͠ΔͨΊɺDaggerͷΑ͏ͳ ίʔυੜ੒͕ඞཁͳ͍

    val appModule = module { single<Hello> { HelloServiceImpl(get()) } } class MyActivity : AppCompatActivity(){ val presenter : MyPresenter by inject() } ؔ਺ʹΑͬͯґଘੑͷ஫ೖ͕Ͱ͖ΔͨΊɺonCreateͳͲͰ໌ࣔతʹ inject injectΛݺͿඞཁ͕ͳ͍ 7min
  10. Koinͱ͸ʁ ɾKotlin੡ͷܰྔDIϥΠϒϥϦ ɾno proxy, no code generation, no reflection! ɾReady

    for ViewModel ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ ɾEasy testing 7min
  11. Koinͱ͸ʁ ɾReady for ViewModel AACͷViewModelΛσϑΥϧτͰαϙʔτ͍ͯ͠Δ class MyActivity : AppCompatActivity(){ //

    Lazy inject MyViewModel val model : MyViewModel by viewModel() } ͷ୅ΘΓʹ by viewModel() by inject() Λ࢖͏͜ͱͰ ViewModelProvider͔ΒViewModelͷΠϯελϯεΛऔಘͯ͘͠ΕΔ ಉҰͷActivityʹଐ͢Δෳ਺ͷFragmentؒͰViewModelΛڞ༗͍ͨ͠ͱ͖͸ɺ୅ΘΓʹ by sharedViewModel() Λ࢖͏ 7min
  12. Koinͱ͸ʁ ɾKotlin੡ͷܰྔDIϥΠϒϥϦ ɾno proxy, no code generation, no reflection! ɾReady

    for ViewModel ɾKotlin DSLΛ࢖ͬͨ؆ܿͳهड़Ͱґଘੑ͕هड़Ͱ͖Δ ɾEasy testing 7min
  13. Koinͱ͸ʁ ɾEasy testing class MyTest : KoinTest { // Lazy

    inject property val componentB : ComponentB by inject() @Test fun `should inject my components`() { startKoin(listOf(appModule))) // directly request an instance val componentA = get<ComponentA>() assertNotNull(a) assertEquals(componentA, componentB.a) } } KoinTestΛܧঝͨ͠ςετΫϥεͷதͰຊ൪ͱಉ༷ʹ startKoin ͕Ͱ͖Δ 7min
  14. Koinͱ͸ʁ ɾEasy testing @Test fun `check module`() { checkModules(listOf(appModule))) }

    Ϟδϡʔϧ಺ͷґଘੑ͕ղܾ͞Ε͍ͯΔ͔ͷςετ΋Ͱ͖Δ 7min
  15. ɾDIͷείʔϓΛίϯτϩʔϧ͍ͨ͠ 3min Dagger → Koin factory, single, scopeؔ਺Λ࢖ͬͯείʔϓΛ੍ޚ͢Δ ΞϓϦέʔγϣϯ୯ҐͰڞ௨ͷΠϯελϯεΛ࢖͏ Dagger

    @Provides @Singleton fun provideRetrofit( okHttpClient: OkHttpClient ): Retrofit Koin single { Retrofit.Builder()… }
  16. ɾDIͷείʔϓΛίϯτϩʔϧ͍ͨ͠ 3min Dagger → Koin factory, single, scopeؔ਺Λ࢖ͬͯείʔϓΛ੍ޚ͢Δ ΧελϜείʔϓΛఆٛ Dagger

    @Scope @Retention(AnnotationRetention.RUNTIME) annotation class ActivityScope @Subcomponent @ActivityScope interface MyActivitySubcomponent { ... } Koin scope(“MyActivity”) { … }
  17. ɾModuleΛ֊૚Խ͍ͨ͠ 3min Dagger → Koin Dagger @Subcomponent interface MyActivitySubcomponent {…}

    @Module(subcomponents = MyActivitySubcomponet::class.java) class MyActivityModule {} @Component(modules = [MyActivityModule::class.java]) interface MyActivityComponent {…} Koin module("MyActivity") { module("MyActivitySub") { … } }
  18. ɾந৅ΫϥεΛ஫ೖ͍ͨ͠ 3min Dagger → Koin Dagger @Binds fun bindUserApi( userApiClient:

    UserApiClient ): UserApi Koin single { UserApiClient() } bind UserApi::class
  19. ɾಉ͡ܕʹผͷΠϯελϯεΛ஫ೖ͍ͨ͠ 3min Dagger → Koin Dagger @Provides @Named("debug-client") fun provideDebugApiClient(retrofit:

    Retrofit): ApiClient @Provides @Named("prod-client") fun provideProdApiClient(retrofit: Retrofit): ApiClient Koin single("debug-client") { ApiClient() } single("prod-client") { ApiClient() }