Advanced Topics

Dynamic overrides, blueprint extension, injection context, semi-dynamic services, and service retrieval.

Dynamic overrides

When creating a container from a blueprint, you can override dynamic services with specific instances. This is essential for request-scoped data in web applications:

class RequestContext(val userId: String)

class UserService(val ctx: RequestContext) {
    fun currentUser() = "User: ${ctx.userId}"
}

val blueprint = kontainer {
    dynamic(RequestContext::class)
    singleton(UserService::class)
}

// Override the dynamic service when creating a container
val kontainer = blueprint.create {
    with(RequestContext::class) { RequestContext("user-42") }
}

println(kontainer.get(UserService::class).currentUser())
// "User: user-42"

Semi-dynamic services

If a singleton injects a dynamic service, Kontainer automatically promotes it to semi-dynamic — it becomes a singleton within each container instance rather than global. This happens transparently; you don't need to do anything.

class RequestContext(val userId: String)

// UserService is defined as singleton, but it injects a dynamic service.
// Kontainer promotes it to semi-dynamic automatically.
class UserService(val ctx: RequestContext)

val blueprint = kontainer {
    dynamic(RequestContext::class)
    singleton(UserService::class)  // automatically becomes semi-dynamic
}

Blueprint extension

Create a new blueprint by extending an existing one. The original is not modified:

val base = kontainer {
    singleton(Counter::class)
}

val extended = base.extend {
    singleton(MyService::class)
}

// 'extended' has both Counter and MyService
// 'base' still only has Counter

Cloning containers

Clone a container to get a fresh instance with reset dynamic services. Optionally apply new overrides:

val kontainer = blueprint.create {
    with(RequestContext::class) { RequestContext("user-1") }
}

// Clone with different dynamic overrides
val cloned = kontainer.clone {
    with(RequestContext::class) { RequestContext("user-2") }
}

Injecting the container and blueprint

The Kontainer itself and the KontainerBlueprint are automatically available as services. Any service can inject them:

class ServiceLocator(val kontainer: Kontainer) {
    fun <T : Any> resolve(cls: KClass<T>): T = kontainer.get(cls)
}

class BlueprintAware(val blueprint: KontainerBlueprint) {
    fun createChild() = blueprint.create()
}

val blueprint = kontainer {
    singleton(ServiceLocator::class)
    singleton(BlueprintAware::class)
}

Injection context

Services can inject InjectionContext to know who is requesting them and what is being injected. Useful for logging and context-aware behavior:

class ContextAwareService(val ctx: InjectionContext) {
    fun whoAsked() = ctx.requestingClass.simpleName
}

Retrieving services

Kontainer provides several ways to get services:

val kontainer = blueprint.create()

// Get — throws if not found
val service = kontainer.get(MyService::class)

// Get with reified type
val service2 = kontainer.get<MyService>()

// GetOrNull — returns null if not found
val maybe = kontainer.getOrNull(MyService::class)

// Use — runs block only if service exists
kontainer.use(MyService::class) {
    println(this.doSomething())
}

// GetAll — all implementations of a type
val allRepos: List<Repository> = kontainer.getAll(Repository::class)

// GetLookup — lazy lookup of implementations
val lookup: LazyServiceLookup<Repository> = kontainer.getLookup(Repository::class)