Skip to content

DI Container library for Kotlin Multiplatform

License

Notifications You must be signed in to change notification settings

chigichan24/koject

 
 

Repository files navigation

Koject [🚧 Work in progress 🚧]

Koject is a DI Container libreary for Kotlin Multiplatform.

fun main() {
    Koject.start()

    val controller = inject<Controller>()
}

@Singleton
@Provides
class Api

@Singleton
@Provides
fun createDB(): DB {
    return DB.create()
}

@Singleton
@Provides
class Repository(
    private val api: Api,
    private val db: DB,
)

@Provides
fun Controller(
    private val repository: Repository
)

Features

Setup

Multiplatform

You need to enable KSP.

build.gradle.kts
plugins {
    kotlin("multiplatform")
+    id("com.google.devtools.ksp") version "<ksp-version>"
}

kotlin {
    android()
    jvm()
    js(IR) {
        nodejs()
        browser()
    }
    ios()

    sourceSets {
        val commonMain by getting {
            dependencies {
+                implementation("com.moriatsushi.koject:koject-core:1.0.0-alpha04")
            }
        }
    }
}

dependencies {
    // Add it according to your targets.
+    val processor = "com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04"
+    add("kspAndroid", processor)
+    add("kspJvm", processor)
+    add("kspJs", processor)
+    add("kspIosX64", processor)
+    add("kspIosArm64", processor)
}

Single platform

Inject can also be used on a single platform.

build.gradle.kts
plugins {
    kotlin("<target>")
+    id("com.google.devtools.ksp") version "<ksp-version>"
}

dependencies {
+    implementation("com.moriatsushi.koject:koject-core:1.0.0-alpha04")
+    ksp("com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04")
}

Library module

Use koject-processor-lib to avoid generating Container in library modules.

build.gradle.kts (Multiplatform)
dependencies {
    // Add it according to your targets.
-    val processor = "com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04"
+    val processor = "com.moriatsushi.koject:koject-processor-lib:1.0.0-alpha04"
    add("kspAndroid", processor)
    add("kspJvm", processor)
    add("kspJs", processor)
    add("kspIosX64", processor)
    add("kspIosArm64", processor)
}
build.gradle.kts (single platform)
dependencies {
    implementation("com.moriatsushi.koject:koject-core:1.0.0-alpha04")
-    ksp("com.moriatsushi.koject:koject-processor-app:1.0.0-alpha04")
+    ksp("com.moriatsushi.koject:koject-processor-lib:1.0.0-alpha04")
}

Usage

Add @Provides annotation to the class you want to provide.

@Provides
class Repository

@Provides
class Controller(
    private val repository: Repository
)

You can get an instance using inject after calling Koject.start().

fun main() {
    Koject.start()

    val controller = inject<Controller>()
}

Provide from functions

Any types can be provided from top-level functions with a @Provides annotation. This is useful when provide outside modules classes.

@Provides
fun createDB(): DB {
    return DB.create()
}

You can also provide from object functions.

object DBFactory {
    @Provides
    fun create(): DB {
        return DB.create()
    }
}

Singleton Scope

By adding the @Singleton annotation, the instance will be created only once and reused within the application.

@Singleton
@Provides
class Api

@Singleton
@Provides
fun createDB(): DB {
    return DB.create()
}

Note that you can't inject a normally scope type into a singleton scope type.

@Provides
class NormalScope

@Singleton
@Provides
class SingletonScope(
    // cannot inject!
    private val normal: NormalScope
)

TODO

This library is incomplete and the following features will be added later.

About

DI Container library for Kotlin Multiplatform

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%