Definitions
By using Koin, you describe definitions in modules. In this section we will see how to declare, organize & link your modules.
Writing a module
A Koin module is the space to declare all your components. Use the module function to declare a Koin module:
In this module, you can declare components as described below.
Defining a singleton
Declaring a singleton component means that Koin container will keep a unique instance of this definition, you describe definitions in modules. In this section we will see how to declare, organize & link your modules.
Defining your component within a lambda
single, factory & scoped keywords help you declare your components through a lambda expression. this lambda describe
the way that you build your component.
..single((s) => MyClass())
The result type of your lambda is the main type of your component
Defining a factory
A factory component declaration is a definition that will gives you a new instance each time you ask for this definition (this instance is not retrained by Koin container, as it won't inject this instance in other definitions later). Use the factory function with a lambda expression to build a component.
info
Koin container doesn't retain factory instances as it will give a new instance each time the definition is asked
Resolving & injecting dependencies
Now that we can declare components definitions, we want to link instances with dependency injection. To resolve an instance in a Koin module, just use the get()
function to the requested needed component instance. This get() function is usually used into constructor, to inject constructor values.
info
To make dependency injection with Koin container, we have to write it in constructor injection style: resolve dependencies in class constructors. This way, your instance will be created with injected instances from Koin.
Let's take an example with several classes:
Definition: binding an interface
A single or a factory definition use the type from the their given lambda definition: i.e single { T }
The matched type of the definition is the only matched type from this expression.
Let's take an example with a class and implemented interface:
You can also use the inferred type expression:
info
This 2nd way of style declaration is preferred and will be used for the rest of the documentation.
Additional type binding
In some cases, we want to match several types from just one definition.
Let's take an example with a class and interface:
Note here, that we would resolve the Service type directly with get(). But if we have multiple definitions binding Service, we have to use the bind<>() function.
Definition: naming & default bindings
You can specify a name to your definition, to help you distinguish two definitions about the same type:
Just request your definition with its name:
get() let you specify a definition name if needed. This name is a qualifier produced by the named() function.
By default Koin will bind a definition by its type or by its name, if the type is already bound to a definition.
Then:
var service = get()will trigger theServiceImpl1definitionvar service = get(named("test"))()will trigger theServiceImpl2definition
Declaring injection parameters
In any single, factory or scoped definition, you can use injection parameter: parameters that will be injected and used by your definition:
In contrary to resolved dependencies (resolved with get()), injection parameters are parameters passed through the resolution API.
This means that those parameters are values passed with get() and by inject().
Using definition flags
The Koin API also proposes some flags.
Create instances at start
A definition or a module can be flagged as createdAtStart, to be created at start (or when you want). First set the createdAtStart flag on your module
or on your definition.
CreateAtStart flag on a definition
CreateAtStart flag on a module
The startKoin function will automatically create definitions instances flagged with createAtStart.
info
If you need to load some definition at a special time (in a background thread instead of UI for example), just get/inject the desired components.