Slide 1

Slide 1 text

©2018 Wantedly, Inc. ,0*/͔Θ͍͍Αɺ,0*/ ,PUMJOѪ޷ձWPM 3ZP4BLBHVDIJ !XBLXBL 2018.9.20 -

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

©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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

©2018 Wantedly, Inc. Dagger

Slide 7

Slide 7 text

©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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

©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

Slide 10

Slide 10 text

©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

Slide 11

Slide 11 text

©2018 Wantedly, Inc. KOIN

Slide 12

Slide 12 text

©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

Slide 13

Slide 13 text

©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

Slide 14

Slide 14 text

©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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

©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

Slide 17

Slide 17 text

©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

Slide 18

Slide 18 text

©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

Slide 19

Slide 19 text

©2018 Wantedly, Inc. InsertKoinIO/koin

Slide 20

Slide 20 text

©2018 Wantedly, Inc. ͓ΘΓ