Insert KOIN for Dependency Injection

| 3 minutes read

KOIN is a dependency injection framework that uses Kotlin and its functional power to get things done! 


TL;DR: We have just released the first version of Koin, an open source Kotlin dependency injection framework: github/koin. This is a more compact repost of my “Better dependency injection for Android” article.


In every dependency injection framework we usually have the same concepts: instances container, reference binding, module definitions, graph dependency… The injection of components is generally done by introspection (scan your class to look for attributes to be injected), which can be costly on mobile devices. That’s why solution such as Dagger emerged with the idea of processing everything and generating source code injection plumbing for you.
On Android development platform, as an alternative to the top used Dagger framework, you can see interesting initiatives like Toothpick that avoid the need for generated code (but use introspection–there is no magic). Keep in mind that all of these solutions have been made in the Java language context.

A few month ago, Sebastien Deleuze proposed a functional way of declaring dependencies into Spring Framework thanks to Kotlin. The gain is quite clear. No need for proxies, introspection or code generation. Just declare your components, assemble it and reuse it thanks to lambda expressions, functions and DSL. All the plumbing behind it is highly simplified, and its global overhead reduced too.

 

The KOIN DSL

Why we are writing a new project when there are already have projects such like Kodein? The proposition of the Koin framework is not dissimilar, but the main idea is behind is to be compact and straight forward to ensure the main value: to allow you to describe your app context in a few lines and reuse it easily everywhere!

This KOIN DSL provides you a simple way to declare your content within lambdas:

The Module class brings the main DSL for your components definition. You can now declare your dependencies in the onLoad() method, within the declareContext function:

  • provide – allows to provide a component in a functional way
  • get – retrieves your component

Note that all your bean definitions are lazily declared. An instance will be made only when an injection is called. We resolve dependencies from context directly with the get() function, which makes bean resolution for you (no need to specify typing, Kotlin is strong enough). You can check the Koin documentation page, for more information on the core features.

Now just build your module context and inject your components from it. That’s all!

 

Insert Koin and start injecting!

Let’s build an Android app with Koin-Android module, an Android specific Koin library. You can find the complete sample app available here. Start with an update of your build.gradle:

// repository if needed
allprojects {
    repositories {
        jcenter()
    }
}
// Koin for Android
dependencies {
   compile 'org.koin:koin-android:0.2.0'
}

Write your module class and describe your dependencies (here we have okhttp, retrofit and a WeatherService class):

The AndroidModule is a Koin module class specialized for Android needs: you can get your injections and applicationContext, resources & assets within your dependencies. e.g: I need my server url here.

Extend the KoinContextAware interface in your Android Application class, to setup your context and provide access to all Android extensions:

That’s it! You’re now ready. Now I can inject my WeatherService class, with the by inject()  function:

Dependency injection done in just a few lines 🙂 No proxy technology, no code generation, no introspection. Just give it a try!

 

What’s next Captain?

Check the Koin project documentation to learn more it. The main target for now, is to help Android developers. That’s why we will look at Android Architecture Components. Next release is on the road!

Feedbacks are welcome 👍

Arnaud Giuliani Author: Arnaud Giuliani

French Java Software Tech, create and run #java #server gears (distributed and other under the hood stuffs). Also like to make #android apps

Like it?  Share  it!

Share Button
What do  You  think? Write a comment!

Leave a Reply

Required fields are marked *.


CommentLuv badge