Koin Components
Sometimes you can't declare only components via Koin. Dependening on your runtime technology, you might need to retrieve instances from Koin in a class that was not created with Koin (e.g. Flutter Widgets)
The KoinComponent interface
Tag your class with the KoinComponent
interface to unlock Koin injection features:
inject()
- lazy inject an instanceget()
- retrieve an instance
We can inject the module above into class properties:
If the class already inherits another one you can use KoinComponentMixin instead of inheriting KoinComponent.
And we just need to start Koin and run our class:
Bootstrapping
KoinComponent
interface is also used to help you bootstrap an application from outside of Koin. Also, you can bringKoinComponent
feature by extension functions directly on some target classes. The koin_flutter pachage use extensions to provide the Koin API for Flutter Widgets.
Bridge with Koin instance
The 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