{ override fun doSomething() { ... } } Binding an interface https://insert-koin.io/docs/1.0/documentation/koin-core/index.html#_binding_an_interface val myModule = module { single { ServiceImp() } single { ServiceImpl() as Service } single<Service> { ServiceImpl() } }
ServiceImpl() } factory { TestA(get()) } factory { TestB(get()) } } class TestA(val service: Service) class TestB(val service: ServiceImpl) (KOIN)::[e] Error while resolving instance for class 'Service' - error: org.koin.error.NoBeanDefFoundException: No compatible de nition found for type 'Service'. Check your module de nition (KOIN)::[e] Error while resolving instance for class 'TestA' - error: org.koin.error.BeanInstanceCreationException: Can't create de nition for 'Factory [name='TestA',class='TestA']' due to error : No compatible de nition found for type 'Service'. Check your module de nition
{ ServiceImpl() } } val service : Service by inject() Naming a definition Exception in thread "main" org.koin.error.BeanOverrideException: Try to override de nition with Single [name='ServiceImpl',class='ServiceImpl'], but override is not allowed. Use 'override' option in your de nition or module.
} Dealing with generics Exception in thread "main" org.koin.error.BeanOverrideException: Try to override de nition with Single [name='ArrayList',class='java.util.ArrayList'], but override is not allowed. Use 'override' option in your de nition or module.
single { ComponentA() } } // de nitions in /org val orgModule = module("org") { single { ComponentB(...) } } // de nitions in /org/sample val sampleModule = module("org.sample") { single { ComponentC(...) } } // de nitions in /org/demo val demoModule = module("org.demo") { single { ComponentD(...) } } Visibility rules CC /sample CA /org CB CD /demo
started or not * allow late module de nition load (e.g: libraries ...) * * @param modules : List of Module */ fun loadKoinModules(modules: List<Module>): Koin Loading modules without startKoin function
Koin property val serviceUrl = getProperty("server_url") // Set a Koin property setProperty("isDebug",false) } } Read/Write property from a KoinComponent Read Write
} single { ComponentB(get()) } } module("ComponentC") { single { ComponentA() } single { ComponentC(get()) } } } val a_b = get<ComponentA>( name = "ComponentB.ComponentA" ) val a_c = get<ComponentA>( name = "ComponentC.ComponentA" ) class ComponentA class ComponentB(val componentA: ComponentA) class ComponentC(val componentA: ComponentA) Resolving instance from a name or a module
View) val myModule = module { single { (view : View) -> Presenter(view) } } class MyComponent : View, KoinComponent { // inject this as View value val presenter : Presenter by inject { parametersOf(this) } }
: 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) } }
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>() } Injecting dependencies
guide ! Inversion of Control Containers and the Dependency Injection pattern ! Moving from Dagger to Koin Simplify your Android development ! What is dependency injection?