declare, start & inject
Below is a short description of
Declare a module
To declare a Koin module, we use the Koin DSL. Below is a short example:
All components described in modules can/should be injected by constructor.
Once you have described your application components in modules, you are ready to start Koin with your list of modules:
From this function, you can specify:
- useEnvironmentProperties - use the properties from your environement
- useKoinPropertiesFile - use the koin.properties file
- extraProperties - additional map of properties
- logger: Logger - Koin logger
- createOnStart: Boolean - create definitions tagged with
Inject with KoinComponents
Tag your class with the
KoinComponent interface to unlock Koin features:
by inject()- lazy inject an instance
get()- retrieve an instance
getProperty()- get a Koin property
release()- release instances from a module
We can inject the module above into class properties:
And we just need to start Koin and run our class:
Using scopes with the Scope API
What’s a scope?
A scope is a fixed duration of time in which an object exists. When the scope context ends, any objects bound under that scope cannot be injected again (they are dropped from the container).
Declare a scope definition
By default in Koin, we have 3 kind of scopes:
singledefinition, create an object that persistent with the entire container lifetime (can’t be dropped).
factorydefinition, create a new object each time. No persistence in the container (can’t be shared).
scopedefinition, create an object that persistent tied to the associated scope lifetime.
To declare a scope definition, use the
Using a scope
KoinComponent class, just use the
getKoin() function to have access to following functions
createScope(id : String)- create a scope with given id in the Koin scope registry
getScope(id : String)- retrieve a previously created scope with given id, from the Koin scope registry
getOrCreateScope(id : String)- create or retrieve if already created, the scope with given id in the Koin scope registry
Once you have created your
Scope instance, let resolve our components!
Once your scope is finished, just closed it with the
Injecting scope definition
If one of your definition need to inject a scope instance, just resolve it and be sure to have created the right scope before:
Just inject it into constructor, with teh right scope:
You can close all the Koin resources and drop instances & definitions. For this you can use the
closeKoin() function from anywhere.
(Deprecated) Releasing single instances from a path
From a KoinComponent, you can the
release() function to release single instances for a given path. For example;
- release(“A”): will release ComponentA, ComponentB & ComponentC
- release(“B”): will release ComponentB & ComponentC
- release(“C”): will release ComponentC
IMPORTANT: This API is deprecated. In next version, Koin won’t allow to drop single. Better use the Scope API
More about core features
Below are some further readings: