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

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.
    ,0*/͔Θ͍͍Αɺ,0*/
    ,PUMJOѪ޷ձWPM
    3ZP4BLBHVDIJ !XBLXBL

    2018.9.20 -

    View Slide

  2. ©2018 Wantedly, Inc.
    Ryo Sakaguchi
    • Wantedly People
    • Android
    • Kotlin/Sake/Beer/Guitar…
    • Twitter/GitHub: @wakwak3125

    View Slide

  3. ©2018 Wantedly, Inc.
    Koin 1.0.0
    Page Title Page Subtitle

    View Slide

  4. ©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

    View Slide

  5. ©2018 Wantedly, Inc.
    How to use?
    Let’s convert from the DI declaration of google/dagger to Koin

    View Slide

  6. ©2018 Wantedly, Inc.
    Dagger

    View Slide

  7. ©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

    View Slide

  8. ©2018 Wantedly, Inc.
    google/Dagger
    @Component(modules = [DaggerModule::class])
    interface AppComponent {
    fun inject(activity: MainActivity)
    }
    Component

    View Slide

  9. ©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

    View Slide

  10. ©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

    View Slide

  11. ©2018 Wantedly, Inc.
    KOIN

    View Slide

  12. ©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

    View Slide

  13. ©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

    View Slide

  14. ©2018 Wantedly, Inc.
    class MainActivity : AppCompatActivity() {
    private val koinWakWak by inject()
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    text2.text = koinWakWak.getMyFavorites()
    }
    }
    Your Activity or Fragment
    KOIN

    View Slide

  15. ©2018 Wantedly, Inc.
    More complex examples
    Sub module, Combined module, Namespace

    View Slide

  16. ©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 { Beer("αοϙϩࠇϥϕϧ") }
    factory { Sake("ਲՆ") }
    factory { Guitar("Fender, Telecaster") }
    module("user") {
    single { WakWak(get(), get(), get()) }
    }
    }
    }
    KOIN Submodule

    View Slide

  17. ©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

    View Slide

  18. ©2018 Wantedly, Inc.
    KOIN Namespace
    class MainActivity : AppCompatActivity() {
    private val aWakWak by inject(name = "A.user.WakWak")
    private val bWakWak by inject(name = "B.user.WakWak")
    private val hello by inject(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

    View Slide

  19. ©2018 Wantedly, Inc.
    InsertKoinIO/koin

    View Slide

  20. ©2018 Wantedly, Inc.
    ͓ΘΓ

    View Slide