Why Koin?

Koin is easy, simple & well documented. It is perfect for encapsulated feature/library modules.

How Koin Works ?

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.

With Koin, setting up dependency injection is kind of a manual process, you essentially plug your services into the module graphs and those dependencies will later get resolved at runtime.

Lines of code

Here are the key functions.

get() – is used inside constructors to resolve needed instances. I advise using named parameters to achieve a better definition & improve readability.

factory – is used to indicate that a new instance must be created each time it is injected.

single – indicates that a unique instance will be created at the beginning and shared on every future injection.

repositoryModule – contains examples of binding interfaces (domain’s repo interface with its actual implementation) you can read more here.

name – is used to name definitions. This is required when you want to have multiple instances of the same class with different types.

bind – additional Kotlin type binding for given bean definition

getProperty() – resolve a property

module { } – create a Koin Module or a submodule (inside a module)

viewModel { } – definition, and this enables you to define a viewModel dependency.

Scope – Koin’s scope allows you to define local scoped-dependencies that are short-lived, like a Presenter, View Adapter, or other service components that are meant to be short-lived within a defined scope, like an Activity’s scope.