KotDil

General

Category
Free
Tag
Dependency Injections
License
N/A
Min SDK
21 (Android 5.0–5.0.2 Lollipop)
Registered
Feb 28, 2019
Favorites
0
Link
https://github.com/vitoksmile/KotDil
See also
Michelangelo
DecleX
google/dagger
Dart
Fudi Annotation

Additional

Language
Kotlin
Version
N/A
Created
Feb 17, 2019
Updated
Apr 6, 2020
Owner
Victor Mykhailiv (vitoksmile)
Contributor
Victor Mykhailiv (vitoksmile)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertising

KotDil

The easy dependency injection framework for Kotlin developers.

Setup

Check that you have the JitPack repository.

// Add JitPack to your repositories if needed
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Pick KotDil dependency:

dependencies {
    implementation 'com.github.vitoksmile:KotDil:2.0.0'
}

How to use KotDil?

The first, create your own modules to provide dependencies:

val idModule = module {
    val id = AtomicInteger(1)
    factory(name = AUTO_ID) {
        id.getAndIncrement()
    }
}
val generatorModule = module {
    single<RandomGenerator> {
        object : RandomGenerator {
            override fun generateLong() = Random.nextLong()
        }
    }
    single<RandomGenerator>(name = RANDOM_FAKE) {
        object : RandomGenerator {
            override fun generateLong() = System.currentTimeMillis()
        }
    }
}

The second, register all modules:

startKotDil {
    modules(idModule, generatorModule)
}

The final step, invoke inject or injectValue to receive some dependency:

val user = User(injectValue(AUTO_ID), "John")

val generator by inject<RandomGenerator>() // Lazy init
generator.generateLong()

Also you can find additional usage examples in the unit-tests package