Skip to content
/ koin Public
forked from InsertKoinIO/koin

KOIN - Functional Kotlin dependency injection framework

License

Notifications You must be signed in to change notification settings

arnis71/koin

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Insert Koin to make dependency injection

KOIN is a dependency injection framework that uses Kotlin and its functional power to get things done! No proxy/CGLib, No code generation, No introspection. Just functional Kotlin and DSL magic ;)

logo

Setup

Check that you have jcenter repository. Add the following gradle dependency to your Android app:

compile 'org.koin:koin-android:0.3.1'

Getting Started

First of all, you need to write a module. A module gathers your components definitions and allows it to be loaded by Koin and injected in your application. Keep in mind, that injection by constructor is the default strategy targeted by Koin. In Android components (Activity, Fragment ...) you can use by inject() to inject your dependencies.  

Check the latest changes: what's new

Setup your Application

To start Koin and your modules, you just have to build it in your application class like below:

class MainApplication : Application(), KoinContextAware {

     // Your Koin Context here
    override val koinContext = newKoinContext(this, allModules())

    override fun onCreate() {
        super.onCreate()
        // Your Koin context is ready ! :)
    }
}

Implement KoinContextAware interface, and build your koinContext with the newKoinContext function huilder. This will able you to use the Koin Android extensions in your Android Application.

The newKoinContext function build a KoinContext for your ApplicationContext class and list of AndroidModule (allmMdules() is just a function returning a list of AndroidModule).

Declare your dependencies

First of all, write a module class (extends AndroidModule), overrides the context() function by using the declareContext function, to declare a context like below:

class MyModule : AndroidModule() {
    override fun context() =
            declareContext {
                provide { ServiceA(get()) }
                provide { ServiceB() }
                provide { ServiceC(get(), get()) }
            }
}

//for classes
class ServiceA(val serviceB: ServiceB) 
class ServiceB()
class ServiceC(val serviceA: ServiceA, val serviceB: ServiceB)

Koin DSL

To describe your module, you can use the following Koin DSL keywords:

  • provide { /* component definition */ } declares a component for your Module - You provide a function to instanciate your component
  • bind {/* compatible type */} bind a compatible type for provided definition (use it behind provide{} expression)
  • get() resolve a component dependency
  • scope {/* scope class */} use the given scope for current module's definitions

NB: Koin is simple: All your components are singletons. You have to use scopes to handle your components lifecycle and release them when needed.

Inject your components

Once your app is configured, you have to ways of handling injection in your application:

  • In Android components (Activity,Fragment...): use the by inject() lazy operator
  • in any Kotlin component: injection is made by constructor

Below a quick sample of injection with by inject() in an Activity:

class MainActivity : AppCompatActivity() {

    // inject my WeatherService 
    val weatherService by inject<WeatherService>()
    // ...
}

The Sample App

The koin-sample-app application offers a complete application sample, with MVP Android style.

The weather app wiki page describes all about Koin features used.

Documentation

A global wiki documentation page gather all features and references about Koin Framework.

Articles

Contact & Support

Slack

Check the kotlin slack community and join #koin channel

Github

Don't hesitate to open an issue to discuss about your needs or if you don't a feature for example.

About

KOIN - Functional Kotlin dependency injection framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 99.6%
  • Java 0.4%