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

Koin MPP入門

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Koin MPP入門

Avatar for RyuNen344

RyuNen344

July 29, 2021
Tweet

More Decks by RyuNen344

Other Decks in Technology

Transcript

  1. What is Koin? • DSLで書ける軽量なDIコンテナライブラリ • 実行時にDependency Graphを構築する ◦ 内部でinstanceをhashMapで管理する

    • android用拡張定義(ViewModel, Service, WorkManger, Compose….)も豊富ですが今日はあんまり触れない • 3.0.0からMPPに対応
  2. Simple Usage // declare dependency val sampleModule = { //

    singleton single { Repository() } // new instance each time factory { Factory() } // instance depends on scope scope { Scoped() } } // constructor injection val sampleModule = { single { Repository(get(), get()) } } // parameter injection val repository: Repository by inject()
  3. // match type ServiceImpl only single { ServiceImpl() } //

    match type Service only single<Service> { ServiceImpl() } // match type Service only single { ServiceImpl() as Service } // match types ServiceImpl & Service single { ServiceImpl() } bind Service::class • koinはKlassをkeyにして instanceをmapで管理 している • mapに複数のkeyに対し て設定できる Type Binding
  4. val scopedModule = module { // scope owner scope<ScopeOwner> {

    // new instance each scope owner scoped { InstancePrinter() } } } • instanceのlifecycleを あるクラスに紐づける • scope ownerは KoinScopeComponen tの実装が必要 Scope
  5. val qualifierModule = module { // string single(named("namedQualifier")) { InstancePrinter()

    } // type single(qualifier<KlassQualifier>()) { InstancePrinter() } // enum single(Mobile.IOS.qualifier) { InstancePrinter() } single(Mobile.ANDROID.qualifier) { InstancePrinter() } } • Klassに追加して情報を 付与してinstance管理さ せる Qualifier
  6. Get instance from container fun <T> get( clazz: KClass<*>, qualifier:

    Qualifier? = null, parameters: ParametersDefinition? = null ) Klassを使ってinstanceを生成する
  7. Get instance from container fun Koin.get(objCClass : ObjCClass, qualifier :

    Qualifier? = null) : Any { val kClazz = getOriginalKotlinClass(objCClass)!! return get(kClazz, qualifier, null) } fun Koin.get(objCProtocol : ObjCProtocol, qualifier : Qualifier? = null) : Any { val kClazz = getOriginalKotlinClass(objCProtocol)!! return get(kClazz, qualifier, null) } こんな拡張関数を作成する
  8. Get instance from container🍎 koin.get(objCClass: Repository.self, qualifier: nil) as! Repository

    koin.get(objCProtocol: Repository.self, qualifier: nil) as! Repository すっきり呼べるようになった🤗
  9. Get instance from container extension KoinApplication { func get<T, R>(prot:

    T, qualifier: Qualifier? = nil) -> R where T : Protocol { return koin.get(objCProtocol: prot, qualifier: qualifier) as! R } func get<T, R>(klass: T, qualifier: Qualifier? = nil) -> R { return koin.get(objCClass: klass as! AnyClass, qualifier: qualifier) as! R } } こんな拡張関数を作成してあげると・・・
  10. Create TypeQualifier fun typeQualifier(objCProtocol : ObjCProtocol) : TypeQualifier { val

    kClazz = getOriginalKotlinClass(objCProtocol)!! return TypeQualifier(type = kClazz) } fun typeQualifier(objCClass : ObjCClass) : TypeQualifier { val kClazz = getOriginalKotlinClass(objCClass)!! return TypeQualifier(type = kClazz) } こんな拡張関数を作成する
  11. Check module definitions class CheckModuleTest : KoinTest { @get:Rule val

    mockProvider = MockProviderRule.create { clazz -> mockkClass(clazz) } @Test fun testExample() { checkModules { modules(sampleModule) } } }