API Reference

Complete public API surface of com.cristianllanos.container.

Core Interfaces

Registrar

Registration of bindings and service providers.

kotlin
interface Registrar {
    fun register(vararg providers: Any): Registrar
    fun <T : Any> factory(type: Class<T>, factory: Container.() -> T): Registrar
    fun <T : Any> singleton(type: Class<T>, factory: Container.() -> T): Registrar
    fun <T : Any> scoped(type: Class<T>, factory: Container.() -> T): ScopedRegistration<T>
}

Resolver

Resolution of dependencies from the container.

kotlin
interface Resolver {
    fun <T : Any> resolve(type: Class<T>): T
}

Caller

Invocation of functions with auto-resolved parameters.

kotlin
interface Caller {
    fun <T> call(callable: KCallable<T>): T
}

Container

Combines registration, resolution, and calling. Creates child scopes.

kotlin
interface Container : Registrar, Resolver, Caller {
    fun child(): Scope
}

Scope

A child container with a closeable lifecycle.

kotlin
interface Scope : Container, AutoCloseable

AutoResolver

Strategy for resolving unregistered types. The default implementation uses reflection on primary constructors. Parameters with default values are skipped when their type cannot be resolved — Kotlin's default value is used instead.

kotlin
interface AutoResolver {
    fun <T : Any> resolve(type: Class<T>, resolver: Resolver): T
}

Container Creation

kotlin
// Basic constructor
fun Container(
    autoResolver: AutoResolver = ReflectionAutoResolver()
): Container

// DSL builder
fun Container(
    autoResolver: AutoResolver = ReflectionAutoResolver(),
    init: Container.() -> Unit,
): Container

Extension Functions

Registration (on Registrar)

kotlin
// With factory lambda
inline fun <reified T : Any> Registrar.factory(noinline factory: Container.() -> T): Registrar
inline fun <reified T : Any> Registrar.singleton(noinline factory: Container.() -> T): Registrar
inline fun <reified T : Any> Registrar.scoped(noinline factory: Container.() -> T): ScopedRegistration<T>

// Auto-resolved (no lambda — resolves via primary constructor)
inline fun <reified T : Any> Registrar.factory(): Registrar
inline fun <reified T : Any> Registrar.singleton(): Registrar
inline fun <reified T : Any> Registrar.scoped(): ScopedRegistration<T>

Resolution (on Resolver)

kotlin
inline fun <reified T : Any> Resolver.resolve(): T
inline fun <reified T : Any> Resolver.resolveOrNull(): T?   // null if unresolvable
inline fun <reified T : Any> Resolver.has(): Boolean         // true if resolvable
inline fun <reified T : Any> Resolver.lazy(): Lazy<T>        // deferred resolution

Scoping (on Container)

kotlin
// Block-based scope with automatic cleanup
inline fun <R> Container.scope(block: (Scope) -> R): R

// Scope with pre-registered providers
inline fun <R> Container.scope(vararg providers: Any, block: (Scope) -> R): R

Types

ScopedRegistration<T>

Returned by scoped() to allow attaching dispose hooks.

kotlin
class ScopedRegistration<T : Any> {
    fun onClose(action: (T) -> Unit): Registrar
}

Exceptions

UnresolvableDependencyException

Thrown when a dependency cannot be resolved — typically a required primitive (String, Int, etc.) or an unregistered interface with no concrete fallback.

ScopeRequiredException

Thrown when a scoped binding is resolved from the root container instead of within a scope.