Skip to main content

Start Koject

To start using Koject, simply add the @Provides annotation to the classes you want to make available for injection.

@Provides
class Repository

@Provides
class Controller(
private val repository: Repository
)

Koject uses the primary constructor to instantiate objects, so it's important to make sure that all parameter types in the primary constructor are annotated with @Provides.

Once you have called Koject.start(), you can obtain an instance of a class by using the inject() method.

fun main() {
Koject.start()

val controller = inject<Controller>()
}

In this case, inject<Controller>() will return an instance of the Controller class that has been instantiated by Koject using its primary constructor and @Provides annotated dependencies.

Provide from functions

The @Provides annotation can also be used in top-level functions and can provide any types. This is especially useful when providing types outside the current module.

@Provides
fun provideDBConfig(): DBConfig {
return DBConfig.Builder().build()
}

@Provides
fun provideDB(dbConfig: DBConfig): DB {
return DB.create(dbConfig)
}

In addition to top-level functions, it is also possible to provide types by functions of objects or companion objects.

By using objects, you can group provide functions.

object DBFactory {
@Provides
fun provideConfig(): DBConfig {
return DBConfig.Builder().build()
}

@Provides
fun provide(dbConfig: DBConfig): DB {
return DB.create(dbConfig)
}
}

By using companion objects, the relationship with a specific type becomes more clear.

class DB {
companion object {
@Provides
fun create(): DB {
return DB()
}
}
}

Singleton Scope

You can use the @Singleton annotation to create an instance only once and reuse it throughout the application. This can be used for dependencies that are expensive to create or dependencies that need to be shared among multiple classes.

@Singleton
@Provides
class Api

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

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

In this case, all three classes (Api, DB, and Repository) are annotated with @Singleton, which means that Koject will create only one instance of each and reuse them throughout the application.

Lazy Injection

By using lazyInject() instead of inject(), you can get Lazy instance and enable lazy injection.

class UIController {
val service: SomeController by lazyInject()

override fun onCreate() {
service.doSomething() // lazy injection
}
}