} class ServiceImpl : Service { override fun doSomething() { ... } } 23 val myModule = module { single { ServiceImp() } single { ServiceImpl() as Service } single<Service> { ServiceImpl() } } https://insert-koin.io/docs/1.0/documentation/koin-core/index.html#_binding_an_interface
- error: org.koin.error.NoBeanDefFoundException: No compatible definition found for type 'Service'. Check your module definition (KOIN)::[e] Error while resolving instance for class 'TestA' - error: org.koin.error.BeanInstanceCreationException: Can't create definition for 'Factory [name='TestA',class='TestA']' due to error : No compatible definition found for type 'Service'. Check your module definition 26 Binding an interface
definition with Single [name='ServiceImpl',class='ServiceImpl'], but override is not allowed. Use 'override' option in your definition or module. 29 Naming a definition
definition with Single [name='ArrayList',class='java.util.ArrayList'], but override is not allowed. Use 'override' option in your definition or module. 32 Dealing with generics
class ComponentA() class ComponentB(val componentA : ComponentA) val moduleA = module { single { ComponentA() } } val moduleB = module { single { ComponentB(get()) } } 46
= module { single { ComponentA() } } // definitions in /org val orgModule = module("org") { single { ComponentB(...) } } // definitions in /org/sample val sampleModule = module("org.sample") { single { ComponentC(...) } } // definitions in /org/demo val demoModule = module("org.demo") { single { ComponentD(...) } } 47 CC /sample CA /org CB CD /demo
Koin modules - whether Koin is already started or not * allow late module definition load (e.g: libraries ...) * * @param modules : List of Module */ fun loadKoinModules(modules: List<Module>): Koin 49
KoinComponent { fun doSomething() { // Read a Koin property val serviceUrl = getProperty("server_url") // Set a Koin property setProperty("isDebug",false) } } 52 Read Write
val module = module { module("ComponentB") { single { ComponentA() } single { ComponentB(get()) } } module("ComponentC") { single { ComponentA() } single { ComponentC(get()) } } } 58 class ComponentA class ComponentB( val componentA: ComponentA) class ComponentC( val componentA: ComponentA) val a_b = get<ComponentA>( name = "ComponentB.ComponentA" ) val a_c = get<ComponentA>( name = "ComponentC.ComponentA" )
: String) val myModule = module { single { (view : View, id : String) -> Presenter(view, id) } } class MyComponent : View, KoinComponent { val id : String ... // inject with view & id val presenter : Presenter by inject { parametersOf(this, id) } } 60
Declare WeatherViewModel with Koin and allow constructor dependency injection */ private val viewModel by viewModel<WeatherViewModel>() } class WeatherFragment : Fragment() { /** Declare shared WeatherViewModel with WeatherActivity */ private val viewModel by sharedViewModel<WeatherViewModel>() } 71
step migration guide ▪ Inversion of Control Containers and the Dependency Injection pattern ▪ Moving from Dagger to Koin — Simplify your Android development ▪ What is dependency injection? ଵҊ 83