Previous release of Koin (0.7.1) was focused on simplification: Koin DSL is now simpler and doesn’t require to put it in a class anymore. API has been fixed and simplified to help you write your DI in the most simple way possible. We always keep in mind: being simply pragmatic.
With release 0.8.0, Koin now has some new modules: koin-spark and koin-android-architecture. Let’s see quickly what they bring.
Koin with Android Architecture
A new module has been created to bring Koin support to Android Architecture Components. Before just add the needed gradle dependency:
- viewModel is a DSL keyword (specialized `provide` alias for ViewModel), to declare an Architecture Components ViewModel
- getViewModel() is a function in Activities and Fragments, to get and bind ViewModel components
Koin provides a factory to Android Architecture API, to bind needed instance from Koin. Just declare a ViewModel class in your module, with viewModel keyword:
Use constructor injection into your ViewModel class (here we inject MyRepository component):
Get it from your Activity or Fragment, with getViewModel():
Voila ! :) You can alsotest your ViewModel classes, and inject it into test classes, with koin-test module.
A sample project has been written to illustrate those features: Android Weather MVVM
Koin for SparkJava (Kotlin)
koin-spark brings some features to help you structure your code with sparkJava. Just add the needed gradle dependency:
- controller is a DSL keyword (specialized `provide` alias), to declare Spark controllers classes
- start() is a function to start Spark server (optional port number) — lambda expression to allow the start of Koin and any controller instantiation
- runControllers() is a function to instantiate all spark controllers, declared with controller keyword DSL
Write a Spark controller (use constructor injection):
Declare it in your module with controller keyword:
Just run it:
Your spark controller classes can be injected in KoinTest classes, with koin-test. A SLF4J logger has been added, to be compatible with Spark logging system.
Check the sample project has been written to illustrate those features: SparkJava Hello WebApp
Sample projects externalized to koin-samples
Sample projects have been moved out to a separated github repository (koin-samples repository) to allow users to just checkout samples and not the whole Koin projects with its complex build system.
Hope those new modules will help you in your Kotlin development. Cheers 👍
Don’t miss our new website. Rendez-vous at insert-koin.io