Image for post
Image for post

Believe it or not, many Android developers have been using DI since their very first applications. However, most new developers haven’t used any DI framework, such as Dagger 2, on their first projects. This sounds ridiculous, but it’s true. How? Let’s see.

In the illustration above, ClassA uses an object instantiated, or created, by itself, whereas ClassB just employs an object instance, regardless of where it comes from. So, it can be stated that DI means that a specific entity or class instance can obtain any dependency it needs from the outer world. In other words, the class is not concerned about how it gets a dependency — just how to use it.

That’s great, but how do you give a dependency on an object? There are two main options:

  • Pass dependencies through the object constructor.
  • Use a DI framework.

Using DI or not using DI

So what are the pros and cons of using DI in your project? To help you with the answer, take a look at the SOLID principles of object-oriented programming, which are five principles that improve the reusability of code and reduce the need to refactor any class. DI is directly related to 2 of these pillars, specifically the Single Responsibility Principle and Dependency Inversion. Briefly explained

The Single Responsibility Principle states that every class or module in a program is responsible for just a single piece of that program’s functionality.

Dependency Inversion states that high-level modules should not depend on low-level modules; both should depend on abstractions.

DI supports these goals by decoupling the creation and the usage of an object. Thus, it allows you to replace dependencies without changing the class that uses them and also reduces the risk of modifying a class because one of its dependencies changed.

This makes DI a great option when an application is expected to grow considerably in size and/or complexity.

Using Koin to Simplify DI

So, why use Koin rather than one of the other DI frameworks? The answer: Koin is more concise and straightforward than the others.

Take the popular Dagger 2 as an example. To use Dagger 2, you first need to become familiar with concepts like module and component and annotations such as @Inject. While the steep learning curve for this framework eventually pays off, to get the most out of it, you still have to learn some advanced concepts such as scope and subcomponents.

In contrast, Koin allows you to simply declare modules, which include potential dependencies, to be used in the project and directly inject them in the class of interest.

Koin Basics

According to the official documentation, you can start using Koin in three simple stages:

  1. Declare a module: Defines those entities which will be injected at some point in the app.

2.Start Koin: A single line, startKoin(this, listOf(applicationModule)), allows you to launch the DI process and indicate which modules will be available when needed, in this case, only applicationModule.

3.Perform an injection:

In consonance with Kotlin features, Koin allows to perform lazy injections in a very convenient way.

One limitation when using Koin is that you can only inject dependencies in Activity classes out of the box. In order to inject dependencies in other class types, you must do it through the corresponding constructors. To solve this problem, Koin allows classes to conform to the KoinComponent interface, so that injections are possible on non-Activity classes. You will see an example of this later on.

As you can see, the above code creates a new Koin module, which includes several important entities. Keep in mind:

1.The module is marked as an override, which means that its content will override any other definition within the application.

2.A factory is a definition that will give you a new instance each time you ask for this object type. In other words, it represents a normal object. Any presenter involved in the application will be injected in the view instance in this way.

3.Single depicts a singleton component such as an instance that is unique across the application. This is typically intended for repositories, databases, etc.

Let’s start coding

In the code lab we will cover the following things:

  1. Add Dependency
  2. Dependency Installation
  3. DI Start Dependency
  4. DI Implementation
  5. Testing
Image for post
Image for post

I am thankful to Raywenderlich for the purposeful tutorial, I took the reference from. The koin is implemented in the above app, which is taken from the Raywenderlich blog and it is created in MVP. Mark me is divided into two features: grading and attendance. Let’s discuss how it’s implemented.

  1. Add Dependency

As you can see, the above code creates a new Koin module, which includes several important entities. Keep in mind:

1.The module is marked as override, which means that its content will override any other definition within the application.

2.A factory is a definition that will give you a new instance each time you ask for this object type. In other words, it represents a normal object. Any presenter involved in the application will be injected in the view instance in this way.

3.Single depicts a singleton component such as an instance that is unique across the application. This is typically intended for repositories, databases, etc.

2. Start Koin: A single line, startKoin(this, listOf(applicationModule)), allows you to launch the DI process and indicate which modules will be available when needed, in this case, the only applicationModule

3.Perform an injection:

In consonance with Kotlin features, Koin allows to perform lazy injections in a very convenient way.

DI implementation in Activity class

DI implementation in Non-Activity class

To implement the DI in non-activity class you have to implement an interface (KoinInterface).

we have completed everything, you can download the code :

I am an android developer, Iot developer, and CEO at liveiot.in

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store