What is KOIN: KOtlin dependency INjection

Overview

Koin is a Dependency injection framework for kotlin. It is written entirely in pure kotlin that’s why it’s so efficient and lightweight and have very good support for android. Are you new to DI? Let’s start from the bottom…

kotlin_image0

What is DI (dependency injection)?

Dependency Injection is a design pattern used to implement inverse of control, meaning the flow of an application is inverted. We can create the dependent object outside of the class and provide those object to class from different ways. DI can help with, moving the creation and binding of the dependent objects outside of the class that depends on them.

kotlin_image1

There are main 4 roles in DI
If you want to use this technique, you need classes that fulfil four basic roles. These are:

  1. The service you want to use.
  2. The client uses the service.
  3. An interface that’s used by the client and implemented by the service.
  4. The injector which makes a service instance and injects it into the client.

I hope you have understood DI and a little bit about koin.

How Koin Works

kotlin_image2

The koin works on simple DSL model. Here we have to create module first. In this module all the dependent objects are there, then we have to load one or more modules in koin. Then, we are ready to use this object. Generally we are loading module in to koin in application class by calling startKoin method,then we can inject the object wherever we want,this how the Koin works.

Koin Vs Dagger

Koin Dagger
Easy to learn and maintain Hard to understand and maintain
Purely written in Kotlin Written in JAVA
Works on DSL module Uses annotation processing
Getting runtime Errors Showing Errors at compile time
Developed by Frech developer Supported by Google
Having a library for ViewModel integration Does not have any special thing to interact with ViewModel
Having a dedicated log on every object creation Does not have any kind of logging feature
Generate less number of line of code Generates more number of line of code then Koin

Setting up Koin

  • Gradle Setup

  • Setup for MVVM extension

Yeee! we have successfully completed the setup

Why to Use Koin?

The very simple answer, to this question, other options are very hard to understand like dagger 2 and having bolipater code, and options like toothpick and many more cannot able to integrate, with ViewModel, Scope Model, Ktor, where as Koin can easily integratable. Koin have it own separate testing module that helps to performing testing.koin uses its own DSL, rather than annotations, Koin DSL Composed of this five things.

  1. Application context
  2. Bean
  3. Factory
  4. Bind
  5. Get

Example

Simple example

  1. We have to create module of dependency that we need to inject

  1. Now we have successfully created a module now we need to pass it to koin for loading it.

  1. Here we are ready to Inject.

All done successfully, this quite simple, isn’t it?

MVVM example

ViewModel Support

Simple integration is just the tip of the iceberg. There are a lot of features that Koin provides. Most importantly, it supports architecture component’s ViewModel. This feature binds Koin more strongly with the android community, no?
Let’s dive into an example to see its usefulness.

  1. Creating ViewModel Class

  1. Creating koin module

  1. Adding module to Koin

  1. Injecting viewModel to Activity

Conclusion

Koin is as fast as dagger yet is easy to learn, it does not have any boilerplate code and it is a very powerful framework for Kotlin developers to use as dependency injection. So what are you waiting for, just implement it and enjoy 🙂

mm

Jignesh Shakya Android developer

I am an android developer at Yudiz Solutions Pvt. Ltd. - a leading Mobile App Development Company having a good command on Kotlin ,and I love flutter and always ready to learn new things and make it better.

Comments are closed.

Top