Koin 0.9.0 — Getting close to stable release
Hi Koin users :) We begin this new month of March with some new Koin content. And again, thank you for your feedback. At the agenda of this last release: dynamic parameters, context scope isolation, better start API, a big upgrade of the ViewModel extensions... and other good things. Let’s go 👍
Upgrade your Gradle as usual with this new release number:
First of all, the provide keyword has been deprecated to leave the room to the bean and factory keywords. Provide is the historical API behind all that, and factory is much shorter to write than provide(isSingleton=false).Now keep in mind:
bean ~ provide => singleton
factory ~ provide(isSingleton=false) => instance factory
It was also weird to have bean and factory limited to definition (and can’t use the bind operator). Now you can use the bind operator on bean and factory definition.
Provide is just deprecated for now. You have the time to update your definitions. It will be passed as internal visibility in a next releases.
One big need that appeared on the Koin@Github repository, is the ability to pass parameters dynamically at runtime, when injecting a component.
Now you can pass parameters as arguments to your definition. Here below, we would like to pass directly our View to our presenter:
Every definition can use this lambda argument, which offers a Key/Value map.
Now, add your parameter in the parameters arguments in your by inject() or by viewModel() function. It can be an interesting way of binding an MVP architecture style:
That’s it. You will pass your parameter at runtime. If you use DryRun() to test your modules, you will have to provide a default parameter else Koin won’t be able to build your instance.
Context scope isolation
Koin has now a clear scope isolation model. When you write a context, you define a space to isolate components instances. This isolation has several purposes:
- visibility — don’t allow components from incompatible scope to inject it
- durability — a context can be dropped with all its instances at any time
Given some scope configuration:
The following visibility rules are applied:
Components from "A" can see components from context "A" and ROOT
Components from "B" can see components from context "A", "B" and ROOT
Components from "C" can see components from context "C" and ROOT
Now, if Koin can’t find one of your bean definition, check your contexts visibility 😎
Context release notification
If you want to listen when a context is dropped, it’s now possible. Just register with registerContextCallBack function:
Now you can react on Koin context drop, and handle more deeply your components lifecycle.
Android silent logging
The Android startKoin() function has now a logger argument, that allows you to define what Koin Logger you want to use if needed (default is set to AndroidLogger()). It can be interesting to set it to EmptyLogger() if you don’t want Koin to log when you’re in runtime.
Android ViewModel API updates
The koin-android-architecture module has been updated to bring some updates to getViewModel() and viewModel():
- can now use parameters argument (like for by inject() functions), to pass dynamic parameters
- can use the key argument, to indicate which instance to reuse from the Android ViewModelProvider
Below a ViewModel example, with dynamic parameter:
Finally, If you can’t use the inferred type version of the API, you can now use the getViewModelByClass() and viewModelByClass() functions, which takes KClass argument to find your ViewModel.
Writing librairies with Koin (unlock loading Koin modules)
Until now, Kotlin didn’t allow to have several “startKoin()” to be used in an app. This means that if you were developing a library and an application with Koin, your were limited.
The startKoin API has been exploded and more opened. You are still limited to only one call of startKoin() in your app, but now you can load Koin modules from your library pars with loadKoinModules() function.
From Java to Kotlin and Koin
The great interest of Koin is its compact syntax to express simple things.
To help people bringing Koin to their project, I suggest a “holder” pattern. The holder consists of a Kotlin class, that will be manually instantiated in your Java activity to bring your Koin stuff. This Kotlin holder then let’s you use the by inject() syntax to lazy inject your components, and all the Kotlin Koin syntax where you can use it. Check below the gists that illustrates this:
Koin has been launched in mid 2017. The main features becomes to remains stable. Next releases will be focused on bug fixes and stability. Being compatible with Espresso for UI testing on Android is one of our “last” big subject for the target stable release.
Next months we will also see if we can propose Koin in tech conference in France :)
Stay tuned 👍