declare, start & inject
Sometimes you can’t declare only components via Koin. Dependening on your runtime technology, you can need to retrieve instances from Koin in a class that is not created with Koin (i.e: Android)
Using the KoinComponent interface
Tag your class with the
KoinComponent interface to unlock Koin injection features:
by inject()- lazy inject an instance
get()- retrieve an instance
getProperty()- get a Koin property
We can inject the module above into class properties:
And we just need to start Koin and run our class:
Bootstrap and runtime extensions
KoinComponent interface is also used to help you boostrap an application from outside of Koin. Also, you can bring
KoinComponent feature by extension functions directly on some tagret classes (i.e: Activity, Fragment have KoinComponent feature in Android).
Bridge with Koin instance & current Scope
KoinComponent interface brings the following:
It opens the following possibilties:
- You can then redefine then
getKoin()function to redirect to a local custom Koin instance.
- You can redefine
currentScopeto give a dedicated Scope instance and avoid to pass it manually in argument in you components.
More about core Koin Components
Below are some further readings: