Android Extended DSL (3.2)
New Constructor DSL (Since 3.2)
Koin now offer a new kind of DSL keyword that allow you to target a class constructor directly, and avoid to to have type your definition within a lambda expression.
Check the new Constructor DSL section for more details.
For Android, this implies the following new constructore DSL Keyword:
viewModelOf()
- equivalent ofviewModel { }
fragmentOf()
- equivalent offragment { }
workerOf()
- equivalent ofworker { }
info
Be sure to use ::
before your class name, to target your class constructor
An Android example
Given an Android application with the following components:
we can declare them like this:
Android Reflection DSL (Deprecated since 3.2)
caution
Koin Reflection DSL is now deprecated. Please Use Koin Constructor DSL above
Koin DSL can be seen as "manual", while you must fill constructors with "get()" function to resolve needed instances. When your definition don't need any special constructor integration (injection paarameters or special scope Id), we can go with more compact writing style thanks to API below.
All injected parameters are also resolved directly with this compact form of writing.
You can freely write viewModel()
, fragment()
or even worker<>()
. All parameters will be passed to your constructor.
note
Using reflection is not costless, even if here it's really minimal. it replaces what you don"t want to write with reflection code (finding primary constructors, injecting parameters...). Mind it before using it, if you are on performances constraints platform (Android for example)