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

KotlinでDI

Bcd7ba3b99b88a1d906961169b467b0a?s=47 kamedon
July 05, 2017

 KotlinでDI

第6回Kotlin勉強会 @ Sansan LT
https://sansan.connpass.com/event/57683/

Bcd7ba3b99b88a1d906961169b467b0a?s=128

kamedon

July 05, 2017
Tweet

Transcript

  1. KotlinͰDI ୈ6ճKotlinษڧձ ˏ Sansan 2017 6/29 Kamedon

  2. ࣗݾ঺հ

  3. ϓϩϑΟʔϧ • Χϝυϯ • Kotlin޷͖Ͱ͢ • ΫϥεϝιουͰΞϯυϩΠυ ͷΞϓϦ։ൃ΍ͬͯ·͢ • ϘʔυήʔϜ͕޷͖Ͱ͢

  4. None
  5. None
  6. Dependency Injection

  7. ೔ຊޠ:Wikipediaͩͱ… lґଘੑͷ஫ೖʢ͍ͦΜ͍ͤͷͪΎ͏ʹΎ ͏ɺӳ%FQFOEFODZJOKFDUJPOʣͱ͸ɺ ίϯϙʔωϯτؒͷґଘؔ܎ΛϓϩάϥϜ ͷιʔείʔυ͔Βഉআ͠ɺ֎෦ͷઃఆϑΝ ΠϧͳͲͰ஫ೖͰ͖ΔΑ͏ʹ͢Διϑτ΢Σ ΞύλʔϯͰ͋Δɻz 8JLJQFEJBґଘੑͷ஫ೖ

  8. ೔ຊޠ:Wikipediaͩͱ… lґଘੑͷ஫ೖʢ͍ͦΜ͍ͤͷͪΎ͏ʹΎ ͏ɺӳ%FQFOEFODZJOKFDUJPOʣͱ͸ɺ ίϯϙʔωϯτؒͷґଘؔ܎ΛϓϩάϥϜ ͷιʔείʔυ͔Βഉআ͠ɺ֎෦ͷઃఆϑΝ ΠϧͳͲͰ஫ೖͰ͖ΔΑ͏ʹ͢Διϑτ΢Σ ΞύλʔϯͰ͋Δɻz 8JLJQFEJBґଘੑͷ஫ೖ ґଘੑͷ஫ೖʁ

  9. ӳޠ൛:Wikipediaͩͱ… l*OTPGUXBSFFOHJOFFSJOH EFQFOEFODZ JOKFDUJPOJTBUFDIOJRVFXIFSFCZPOF PCKFDUTVQQMJFTUIFEFQFOEFODJFTPG BOPUIFSPCKFDU"EFQFOEFODZJTBO PCKFDUUIBUDBOCFVTFE BTFSWJDF z 8JLJQFEJB%FQFOEFODZJOKFDUJPO

  10. ӳޠ൛:Wikipediaͩͱ… l*OTPGUXBSFFOHJOFFSJOH EFQFOEFODZ JOKFDUJPOJTBUFDIOJRVFXIFSFCZPOF PCKFDUTVQQMJFTUIFEFQFOEFODJFTPG BOPUIFSPCKFDU"EFQFOEFODZJTBO PCKFDUUIBUDBOCFVTFE BTFSWJDF z 8JLJQFEJB%FQFOEFODZJOKFDUJPO

    dependency͸ΦϒδΣΫτ ΦϒδΣΫτͷ஫ೖ
  11. Πϝʔδ͠Α͏ DIͬΆ͍ͷ͸Ͳͬͪʁ ۩ࡐɺຯΛௐ੔ɺม͑΍͍͢ͷ͸

  12. Πϝʔδ͠Α͏ DIͬΆ͍ͷ͸Ͳͬͪʁ ۩ࡐɺຯΛௐ੔ɺม͑΍͍͢ͷ͸

  13. ιʔείʔυͩͱ class FriedRice val friedRice = FriedRice() class BowlOFRice(val rice:

    Rice, val topping: Topping) interface Topping interface Rice class JapaneseRice : Rice class BeefTopping : Topping //オブジェクトを入れる val beefBowl = BowlOFRice(JapaneseRice(), BeefTopping()) ※ઃܭ࣍ୈͰνϟʔϋϯ΋DIԽͰ͖·͢
  14. DIͷࢫຯɿมߋʹڧ͍ • ஫ೖ͞ΕΔ΋ͷ͸ΦϒδΣΫτ͕ͩ
 ΠϯλʔϑΣʔεʢ࢓༷ʣʹґଘͨ͠΄͏͕ΑΓॊೈ • ڇက͡Όͳͯ͘ɺ;Γ͔͚ကͰ΋໰୊ͳ͠ • ܧঝͰͳ͘ɺ࢓༷ʹґଘ͍ͯ͠ΔͨΊՄೳ • ςετ(ຯݟ)ͷͱ͖ʹϥΠεΛϞοΫʹ͢Δ͜ͱ

    ͰɺຯݟՄೳ
  15. DI Container (Dependency Injection Container)

  16. DIͱDI Container • DI Container͸
 DIΛ࣮ݱ͢ΔͨΊͷศརπʔϧͰ͔͠ͳ͍ • ઌఔͷઆ໌Ͱ
 DaggerͳͲπʔϧ͕Ͱ͖ͯ·͔ͨ͠ʁ class

    BowlOFRice(val rice: Rice, val topping: Topping) //オブジェクトを入れる val beefBowl = BowlOFRice(JapaneseRice(), BeefTopping())
  17. DI Containerͷࢫຯ • ࠷େͷϞνϕʔγϣϯɿΊΜͲ͏͍͘͞ • ͍͍ͪͪΦϒδΣΫτ࡞੒͢Δͷ͕໘౗ • ౎౓new͢Δඞཁ͕ͳ͘࢖͍·Θ͍ͨ͠ • ஫ೖ͢ΔΦϒδΣΫτΛมߋ͢Δ࣌ʹɺ


    ͢΂ͯม͑Δͷ໘౗ ґଘؔ܎Λهड़ͨ͠Β উखʹੜ੒ɺ஫ೖͯ͘͠ΕͨΒ͍͍ͷʹ
  18. DI Containerͷࢫຯ • ࠷େͷϞνϕʔγϣϯɿΊΜͲ͏͍͘͞ • ͍͍ͪͪΦϒδΣΫτ࡞੒͢Δͷ͕໘౗ • Θ͟Θ͟new͢Δඞཁ͕ͳ͘࢖͍·Θ͍ͨ͠ • ஫ೖ͢ΔΦϒδΣΫτΛมߋ͢Δ࣌ʹɺ


    ͢΂ͯม͑Δͷ໘౗ ґଘؔ܎Λهड़ͨ͠Β উखʹੜ੒ɺ஫ೖͯ͘͠ΕͨΒ͍͍ͷʹ DI Container͞Μ͓ئ͍͠·͢
  19. Πϝʔδ͠Α͏ ڇက DIίϯςφ ΅͘Β͸ڇကΛఆٛ

  20. DI Container? Service Locator? ͕͢͞ʹຊ୊ʹೖΓ·͢ɻ

  21. Android DI: Dagger2?

  22. DIͷ֓೦ͷΘΓʹෳࡶ • ίϯϙʔωϯτɺαϒίϯϙʔωϯτɺϞδϡʔ ϧɺείʔϓΛద੾ʹఆٛ͢Δͷ͕େม • DIͷྖҬ͔ΒAndroidྖҬʹ౿ΈࠐΜͰ͖͍ͯΔ • Activity,Fragment͕ੜ੒͞Εͨޙ
 Ͱ͔ࠩ͠͠ࠐΊͳ͍΋ͷఆ͕ٛ໘౗ •

    kaptͱDagger2ͱͷ૬ੑ͕ѱ͍ • kvs-schemaͳͲͰ࡞ΒΕΔΫϥεΛProviderͰ ͖ͳ͍ɻʢHolderͳͲ࡞Ε͹Մೳʣ
  23. KotlinͳΒҧ͏ํ๏͕͋Δ͔΋ • ྫ͑͹ɺDelegationɺDelegated PropertiesΛ ࢖͑͹΋ͬͱ͏·͘Ͱ͖Δ͔΋ class Delegate {
 operator fun

    getValue(thisRef: Any?, property: KProperty<*>): String {
 return "$thisRef, thank you for delegating '${property.name}' to me!"
 }
 
 operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
 println("$value has been assigned to '${property.name} in $thisRef.'")
 }
 } 
 class Example {
 var p: String by Delegate()
 }
  24. Kotlin + Android DI: Kodein

  25. • Kotlin੡ • kaptΛ࢖Θͳ͍ͷͰɺϏϧυ஗͘ͳΒͳ͍ • KotlinͷػೳΛ͏·͘࢖͍ͬͯΔ • DelegationΛ࢖ͬͯɺ͏·͘஫ೖ͍ͯ͠Δ • ௚ײతͳจ๏

    Kodein
  26. Kodein͕Ͱ͖Δ͜ͱ • Lazily instantiate your dependencies when needed • Stop

    caring about dependency initialization order • Easily bind classes or interfaces to their instance or provider • Easily debug your dependency bindings and recursions SalomonBrys/KodeinͷREADME.mdΑΓ
  27. Kodein͕Ͱ͖Δ͜ͱ • Lazily instantiate your dependencies when needed • Stop

    caring about dependency initialization order • Easily bind classes or interfaces to their instance or provider • Easily debug your dependency bindings and recursions SalomonBrys/KodeinͷREADME.mdΑΓ •ඞཁͳ࣌ʹΠϯελϯεԽ •providerʹ
 ΫϥεɺΠϯλʔϑΣʔεɺΠϯελϯεΛઃఆͰ͖Δ
  28. Moduleఆٛ Kodein.Module { bind<型>(Qualifier) with singleton { … } bind<型>(Qualifier)

    with provider { … } bind<型>(Qualifier) with instance ( … ) } • Dagger2ͷModuleͱಉ͡ • KotlinͷػೳΛϑϧʹ࢖͍ͬͯͯจ๏ͰಡΈ΍͍͢ • infixɺϥϜμলུɺϨγʔόʔࢦఆϥϜμ
  29. Moduleྫ object NetworkModule { val module = Kodein.Module { bind<OkHttpClient>()

    with singleton { OkHttpClient.Builder().build() } bind<Converter.Factory>() with singleton { GsonConverterFactory.create() } bind<Retrofit>() with singleton { Retrofit.Builder() .baseUrl(BuildConfig.GITHUB_ENDOPOINT) .addConverterFactory(instance()) .client(instance()).build() } } }
  30. Moduleྫ object NetworkModule { val module = Kodein.Module { bind<OkHttpClient>()

    with singleton { OkHttpClient.Builder().build() } bind<Converter.Factory>() with singleton { GsonConverterFactory.create() } bind<Retrofit>() with singleton { Retrofit.Builder() .baseUrl(BuildConfig.GITHUB_ENDOPOINT) .addConverterFactory(instance()) .client(instance()).build() } } }
  31. Kodeinͷఆٛ class App : Application(), KodeinAware { override val kodein:

    Kodein = Kodein { bind<Context>() with instance(this@App) import(NetworkModule.module) import(PresentationModule.module) } } • Dagger2ͷComponentͱಉ͡ • ࢖༻͢ΔϞδϡʔϧΛఆٛ͢Δ
  32. Inject class MVPActivity : AppCompatActivity(), GithubView { val binding by

    lazy { DataBindingUtil.setContentView<…>(this, R.layout…) } val injector: KodeinInjector = KodeinInjector() val presenter: GithubPresenter by injector.instance() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) injector.inject(Kodein { extend(kodein) bind<GithubView>() with instance(this@MVPActivity) }) binding.searchBtn.setOnClickListener { presenter.search(binding.userEdit.text.toString()) } } }
  33. Dagger2 ͱ Kodein ൺֱ

  34. Dagger2 ͱ Kodein ,PEFJO %BHHFS ґଘؔ܎Λ LBQUͰੜ੒ ෆཁ ඞཁ BQUͰੜ੒͞Ε

    ͨ΋ͷJOKFDU ̋ ˚ )PMEFSΛ࡞ΔͳͲ ґଘؔ܎ͷ Τϥʔ ࣮ߦ࣌ ίϯύΠϧ࣌ είʔϓ ̋ ̋ /BNFE 2VBMJpFS ̋ ̋
  35. ґଘؔ܎͕͓͔͍࣌͠ͷΤϥʔ • Daggerͷ৔߹͸ɺίϯύΠϧ࣌ʹΤϥʔ • aptͰੜ੒͢ΔͷͰɺґଘؔ܎͕͓͔͍͠৔߹ʹ͸ ίϯύΠϧͰ͖·ͤΜɻ • Kodeinͷ৔߹͸ɺ࣮ߦ࣌ʹΤϥʔ • MapΈ͍ͨͳ΋ͷʹɺΦϒδΣΫτΛಥͬࠐΜͰऔ

    Γग़ͯ͠Δ͚ͩͳͷͰɺinject͢Δͱ͖ʹͭͬ͜· Ε͍ͯΔͷ͔Ͳ͏͔͸ɺ࣮ߦ͞ΕΔ·ͰΘ͔Βͳ͍ ͨΊͰ͢ɻ
  36. ·ͱΊ • DIπʔϧ:Dagger2Ҏ֎ͷબ୒΋͋Δ͔΋ • DelegationΛ࢖ͬͯDIΛ࣮ݱͰ͖ͦ͏ • Kotlin͕Android։ൃݴޠͱͯ͠ೖͬͨ͜ͱͰఆ൪ϥ ΠϒϥϦͷมಈ͕͋Δ͔΋͠Εͳ͍… • Kodein͸Service

    Locatorͱͯ͠࢖͍΍͍͢ͷͰ஫ҙ
  37. αϯϓϧ • KodeinSample • https://github.com/kamedon/ KodeinSample

  38. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠