使い方
- interface Heater
- interface Pump
- class ElectricHeater : Heater
- class Thermosiphon(private val heater: Heater) : Pump
- class CoffeeMaker(val heater:Heater, val pump:Pump)
1. dependency
- implementation 'org.koin:koin-android:0.6.0'
- testImplementation 'org.koin:koin-test:0.6.0'
2. AndroidModule を継承したクラスを用意し、context() メソッドを実装する
この Context は Android の Context ではなくて org.koin.dsl.context.Context です。 Context の applicationContext() を使って構成します。- class DripCoffeeModule : AndroidModule() {
- override fun context(): Context {
- return applicationContext {
- provide { ElectricHeater() } bind Heater::class
- provide { Thermosiphon(get()) } bind Pump::class
- provide { CoffeeMaker(get(), get()) }
- }
- }
- }
- fun applicationContext(init: Context.() -> Unit) = Context(Scope.ROOT, koinContext).apply(init)
- provide(isSingleton = false) { CoffeeMaker(get(), get()) }
- provideFactory { CoffeeMaker(get(), get()) }
- provide("Coffee") { CoffeeMaker(get(), get()) }
- provide("Coffee2") { CoffeeMaker(get(), get()) }
- class DripCoffeeModule : AndroidModule() {
- override fun context(): Context {
- return applicationContext {
- context("MainActivity") {
- provide { ElectricHeater() } bind Heater::class
- provide { Thermosiphon(get()) } bind Pump::class
- provide { CoffeeMaker(get(), get()) }
- }
- }
- }
- }
- provide { ElectricHeater(androidApplication) } bind Heater::class
3. アプリケーションクラスの onCreate() で startKoin() を呼ぶ
- class MyApplication : Application() {
- override fun onCreate() {
- super.onCreate()
- startKoin(this, listOf(DripCoffeeModule()))
- }
- }
4. inject
- class MainActivity : Activity() {
- val maker by inject<CoffeeMaker>()
- }
- inline fun <reified T> ComponentCallbacks.inject(name: String = "") = lazy { (StandAloneContext.koinContext as KoinContext).get<T>(name) }
- class MainActivity : Activity() {
- val maker by lazy { (StandAloneContext.koinContext as KoinContext).get<CoffeeMaker>() }
- }
- class MainActivity : Activity() {
- private lateinit var maker: CoffeeMaker
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- maker = (StandAloneContext.koinContext as KoinContext).get()
- }
- }
provide で name を指定した場合、inject にその name を指定して取得できます。
- class MainActivity : Activity() {
- val maker by inject<CoffeeMaker>("Coffee")
- }
- override fun onDestroy() {
- super.onDestroy()
- releaseContext("MainActivity")
- }
- class TestActivity : ContextAwareActivity("MainActivity", ContextDropMethod.OnDestroy) {
- val maker by inject<CoffeeMaker>()
- }
おまけ、Daggerの場合
- kapt 'com.google.dagger:dagger-compiler:2.11'
- implementation 'com.google.dagger:dagger:2.11'
- interface Heater
- interface Pump
- class ElectricHeater : Heater
- class Thermosiphon @Inject constructor(private val heater: Heater) : Pump
- class CoffeeMaker @Inject constructor(val heater: Heater, val pump: Pump)
- @Module
- class DripCoffeeModule {
- @Provides
- fun provideHeater(): Heater {
- return ElectricHeater()
- }
- @Provides
- fun providePump(pump: Thermosiphon): Pump {
- return pump
- }
- }
- @Component(modules = arrayOf(DripCoffeeModule::class))
- interface CoffeeShop {
- fun maker(): CoffeeMaker
- }
- val coffeeShop = DaggerCoffeeShop.builder()
- .dripCoffeeModule(DripCoffeeModule())
- .build()
- val maker = coffeeShop.maker()
- assertThat(maker.heater).isNotNull()
- assertThat(maker.pump).isNotNull()