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

KotlinでDI

kamedon
July 05, 2017

 KotlinでDI

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

kamedon

July 05, 2017
Tweet

More Decks by kamedon

Other Decks in Technology

Transcript

  1. ιʔείʔυͩͱ 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ԽͰ͖·͢
  2. DIͱDI Container • DI Container͸
 DIΛ࣮ݱ͢ΔͨΊͷศརπʔϧͰ͔͠ͳ͍ • ઌఔͷઆ໌Ͱ
 DaggerͳͲπʔϧ͕Ͱ͖ͯ·͔ͨ͠ʁ class

    BowlOFRice(val rice: Rice, val topping: Topping) //オブジェクトを入れる val beefBowl = BowlOFRice(JapaneseRice(), BeefTopping())
  3. 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()
 }
  4. 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ΑΓ
  5. 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ʹ
 ΫϥεɺΠϯλʔϑΣʔεɺΠϯελϯεΛઃఆͰ͖Δ
  6. Moduleఆٛ Kodein.Module { bind<型>(Qualifier) with singleton { … } bind<型>(Qualifier)

    with provider { … } bind<型>(Qualifier) with instance ( … ) } • Dagger2ͷModuleͱಉ͡ • KotlinͷػೳΛϑϧʹ࢖͍ͬͯͯจ๏ͰಡΈ΍͍͢ • infixɺϥϜμলུɺϨγʔόʔࢦఆϥϜμ
  7. 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() } } }
  8. 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() } } }
  9. Kodeinͷఆٛ class App : Application(), KodeinAware { override val kodein:

    Kodein = Kodein { bind<Context>() with instance(this@App) import(NetworkModule.module) import(PresentationModule.module) } } • Dagger2ͷComponentͱಉ͡ • ࢖༻͢ΔϞδϡʔϧΛఆٛ͢Δ
  10. 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()) } } }
  11. Dagger2 ͱ Kodein ,PEFJO %BHHFS ґଘؔ܎Λ LBQUͰੜ੒ ෆཁ ඞཁ BQUͰੜ੒͞Ε

    ͨ΋ͷJOKFDU ̋ ˚ )PMEFSΛ࡞ΔͳͲ ґଘؔ܎ͷ Τϥʔ ࣮ߦ࣌ ίϯύΠϧ࣌ είʔϓ ̋ ̋ /BNFE 2VBMJpFS ̋ ̋