When Koin met Ktor…

| 4 minutes read

As a Kotlin developer and great fan of “clean code”, I’m always wandering the Internet to find new cool frameworks to have more fun with and be more productive. Most of the backend projects I’m involved with at work are related to API services and micro-services oriented architectures. So it’s not a big surprise to have spotted the Ktor project which aims to provide to developers a framework for quickly creating web applications in Kotlin with minimal effort.

Ktor is a framework developed by Jetbrains. They’re also developing the Kotlin language… It seems to be a good start. 😉
Ktor is developed in Kotlin and provides a very elegant code taking advantage of Kotlin’s expressivity.

The snippet above shows how we can develop self-hosted applications which is great. Ktor is not bound to Servlet containers such as tomcat, jetty, etc. Non-blocking servers like Netty can also be used as Ktor’s backbone.
This is uncommon feature is quite interesting. We can develop applications based on HTTP, HTTP/2 or sockets and the internal Ktor mechanism relies on an asynchronous Pipeline implemented with Kotlin coroutines.

Last but not least, Ktor is designed in a really modular way allowing us to include only requested dependencies and not a bunch of non-used JARs. It reminds me the very good Feign HTTP client framework in the Java world.

Koin aims to be as simple as possible and lets you write dependency injection in a breathJust describe your stuff and inject it! See GitHub project.

Developed by Arnaud Giuliani at ekito and initially intended for Android developers, Koin project has evolved during last few months in order to focus and extract its core concepts to make them available to any Kotlin application developers.

When Koin met Ktor…

Ktor is an unopinionated framework which doesn’t impose technological choices for logging, templating, serialization, messaging and dependency injection mechanism.

This design choice is really cool! Developers can use Ktor alongside with their frameworks or within a more constraint environment where some frameworks choice is imposed.

At ekito, Kotlin developers are using Koin framework to perform dependency injection in very simple way since several months now. So, the queston is: is it possible to use Koin with Ktor applications?

TL;DR the answer is yes ! Of course it’s possible to use Koin within Ktor applications. It’s ready out-of-the-box! “Declare! Start! Inject the Kool way!”  

1- Let’s write a Koin module: « Declare ! »

Koin modules are used to describe dependency injection stuff declaratively with a DSL. This is the starting point for a project using Koin.

The class KoinModule declares a binding definition for the BusinessService interface and its corresponding implementation (BusinessServiceImpl class) in the applicationContext (which is the Koin root context). After that, we will be able to inject service implementation simply like that :

Note some properties names are also declared here in order to avoid typo or spelling bugs when we will use properties.

2- Let’s start Koin: « Start ! »

Once Koin modules are well defined, the next step is to start Koin by using startKoin() static method. This is the right time to eventually set initial properties as depicted in the snippet below. Note that Koin properties values are not just limited to a String but can be any object. So let’s start Koin alongside Ktor embedded server.

3- Time to inject into Ktor routing: « Inject the Kool way ! »

Now, Koin framework is initialized, and ready to be used. Let’s inject the BusinessService implementation into a Ktor module.

The snippet above illustrates how the service is injected only based on the interface type. We can also see how property can be retrieved using getProperty function and how this property can be updated from the code by using setProperty() function.

4- And what about outside Ktor Application?

We can require dependency injection into some classes which are not related to Ktor Application. How can we manage that?

In fact, this is the typical Koin use case for standalone Kotlin applications. We will have to “mark” the intended class by implementing the KoinComponent marker interface. And.. voila !

5- Did you say test?

Both Ktor and Koin provides testing capabilities which can be used out-of-the box in your project as depicted below.

To have details about testing you can have a look at Ktor testing capabilities here: http://ktor.io/application/testing.html and Koin testing capabilities here: https://github.com/Ekito/koin#testing.

Wrap-up

To conclude this walk through the world of Ktor and Koin we can say that they are both completing each other very well with the same shared philosophy: Simple, Elegant, Fun and Smart!

 

Project source code available on Github: https://github.com/Ekito/koin/tree/master/koin-ktor

Like it?  Share  it!

Share Button
What do  You  think? Write a comment!

Leave a Reply

Required fields are marked *.


CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.