interface IContainer
Since 0.1.0

Dependency injection container.

Examples

Example 1

const container = ctx.services.get<IContainer>(CAPABILITIES.DI_CONTAINER);
container.register('UserService', {
  useClass: UserService,
  inject: [CAPABILITIES.DATABASE, CAPABILITIES.LOGGER],
});
const users = container.resolve<UserService>('UserService');

Methods

register<T>(
token: string,
provider: Provider<T>,
options?: ProviderOptions
): void

Registers a provider under a token.

resolve<T>(token: string): T

Resolves an instance, constructing it (and its dependencies) as needed.

has(token: string): boolean

Reports whether a token is registered.

Creates a child scope. Scoped services resolve to one instance per scope; singletons are shared with the parent.

Usage

import { type IContainer } from "common/src/index.ts";