Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

KOINかわいいよ、KOIN

Ryo Sakaguchi
September 20, 2018

 KOINかわいいよ、KOIN

Kotlin愛好会 Vol.4 2018/09/20 #love_kotlin

Ryo Sakaguchi

September 20, 2018
Tweet

More Decks by Ryo Sakaguchi

Other Decks in Programming

Transcript

  1. ©2018 Wantedly, Inc. Ryo Sakaguchi • Wantedly People • Android

    • Kotlin/Sake/Beer/Guitar… • Twitter/GitHub: @wakwak3125
  2. ©2018 Wantedly, Inc. 1. Lightweight 2. No Proxy/No Code Generation/(No)Reflection

    3. Simple DSL 4. Multi Platform • Kotlin/Java Android(with AAC ViewModel support) • Standalone application • Spark • Ktor About Koin
  3. ©2018 Wantedly, Inc. How to use? Let’s convert from the

    DI declaration of google/dagger to Koin
  4. ©2018 Wantedly, Inc. google/Dagger @Module class DaggerModule { @Provides fun

    provideBeer(): IBeer = Beer("BREWDOG, PUNK IPA") @Provides fun provideSake(): ISake = Sake("ُઘ") @Provides fun provideGuitar(): IGuitar = Guitar("Fender, Stratocaster") @Provides @Singleton fun provideWakWak(beer: IBeer, sake: ISake, guitar: IGuitar): WakWak = WakWak(beer, sake, guitar) } Module
  5. ©2018 Wantedly, Inc. google/Dagger class KoinDaggerApp : Application() { lateinit

    var component: AppComponent private set override fun onCreate() { super.onCreate() component = DaggerAppComponent.create() } } Your Application class
  6. ©2018 Wantedly, Inc. google/Dagger class MainActivity : AppCompatActivity() { @Inject

    lateinit var daggerWakWak: WakWak override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) (application as KoinDaggerApp).component.inject(this) text1.text = daggerWakWak.getMyFavorites() } } Your Activity or Fragment
  7. ©2018 Wantedly, Inc. KOIN class KoinDaggerApp : Application() { override

    fun onCreate() { super.onCreate() startKoin(this, listOf(appModule)) } private val appModule = module { factory { Guitar("Fender, Telecaster") as IGuitar } factory { Beer("αοϙϩࠇϥϕϧ") as IBeer } factory { Sake("ਲՆ") as ISake } single { WakWak(get(), get(), get()) } } } Your Application class
  8. ©2018 Wantedly, Inc. class KoinDaggerApp : Application() { override fun

    onCreate() { super.onCreate() startKoin(this, listOf(appModule)) } private val appModule = module { factory { Guitar("Fender, Telecaster") as IGuitar } factory { Beer("αοϙϩࠇϥϕϧ") as IBeer } factory { Sake("ਲՆ") as ISake } single { WakWak(get(), get(), get()) } } } KOIN Your Application class
  9. ©2018 Wantedly, Inc. class MainActivity : AppCompatActivity() { private val

    koinWakWak by inject<WakWak>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) text2.text = koinWakWak.getMyFavorites() } } Your Activity or Fragment KOIN
  10. ©2018 Wantedly, Inc. class KoinDaggerApp : Application() { inner class

    Message(val message: String) override fun onCreate() { super.onCreate() startKoin(this, listOf(appModule)) } private val appModule = module { module("A") { // Submodule factory<IBeer> { Beer("αοϙϩࠇϥϕϧ") } factory<ISake> { Sake("ਲՆ") } factory<IGuitar> { Guitar("Fender, Telecaster") } module("user") { single { WakWak(get(), get(), get()) } } } } KOIN Submodule
  11. ©2018 Wantedly, Inc. class KoinDaggerApp : Application() { inner class

    Message(val message: String) override fun onCreate() { super.onCreate() startKoin(this, listOf(messageModule, appModule)) } private val messageModule = module { factory { Message("Hello :)") } } private val appModule = module { module("A") { ɹɹ //… module("user") { single { WakWak(get(), get(), get()) } } } } } KOIN Combined module
  12. ©2018 Wantedly, Inc. KOIN Namespace class MainActivity : AppCompatActivity() {

    private val aWakWak by inject<WakWak>(name = "A.user.WakWak") private val bWakWak by inject<WakWak>(name = "B.user.WakWak") private val hello by inject<KoinDaggerApp.Message>(name = "Hello.Message") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) text1.text = aWakWak.getMyFavorites() text2.text = bWakWak.getMyFavorites() • A.user.WakWak = ModuleName.ModuleName.ClassName • Hello.Message = ModuleName.ClassName