Minimal Dependency Injection container.

Example

import { Container } from "@markeasting/dependency-injection";

const container = new Container();

// Or import a globally available container instance directly:
// import { container } from "@markeasting/dependency-injection";

// 1) LoggerService depends on some log level constant
container.transient(LoggerService, [LogLevel.DEBUG]);

// 2) Database depends on an instance of LoggerService
container.singleton(Database, [LoggerService]);

// 3) Register 'MyApp'
container.singleton(MyApp, [Database, LoggerService]);

// 3) Build the container
container.build();

// Get an instance - dependencies will be injected here
const app = container.get(MyApp);

Hierarchy (view full)

Constructors

Properties

parameters: Record<string, any> = {}
dependencies: Map<ClassType<any>, ClassType<any>[]> = ...
services: Map<ClassType<any>, any> = ...
overides: Map<ClassType<any>, ClassType<any>> = ...
#serviceLifetimes: Map<ClassType<any>, Lifetime> = ...
#compiled: boolean = false

Methods

  • Registers a class as a service. You can also use singleton and transient.

    Throws ArgumentCountError if the given dependencies do not match the required constructor arguments.

    Throws CyclicalDependencyError if there is a self-referencing constructor argument.

    Type Parameters

    Parameters

    • ctor: T

      The service to register - can be any class.

    • dependencies: MapDependencies<ConstructorParameters<T>>

      Dependencies (constructor parameters) of the service

    • Optional lifetime: Lifetime = Lifetime.SHARED

    Returns this

    Example

    // Logger depends on some log level constant
    container.register(LoggerService, [LogLevel.DEBUG]);

    // Database depends on Logger
    container.register(Database, [LoggerService]);
  • Registers a class as a singleton service.

    Shortcut for register with Lifetime.SHARED.

    Type Parameters

    Parameters

    • ctor: T

      The service to register - can be any class.

    • dependencies: MapDependencies<ConstructorParameters<T>>

      Dependencies (constructor parameters) of the service

    Returns this

  • Registers a class as a transient service.

    Shortcut for register with Lifetime.TRANSIENT.

    Type Parameters

    Parameters

    • ctor: T

      The service to register - can be any class.

    • dependencies: MapDependencies<ConstructorParameters<T>>

      Dependencies (constructor parameters) of the service

    Returns this

  • Initializes the container, after which you can request instances via get.

    • Applies implementation overrides that were set via override.

    Returns this

  • Overrides a service with another concrete implementation.

    Note: Javascript does not support interfaces (i.e. you cannot pass a TS interface by value). Therefore, you should first register() a 'base class' as a default implementation, after which you can override it using override().

    You may omit the 'dependencies' argument to match the constructor signature of the overridden class.

    Type Parameters

    Parameters

    • ctor: T
    • overrideCtor: T
    • Optional dependencies: MapDependencies<ConstructorParameters<T>>

    Returns this

    Example

    class IFoo {
    someMethod(): void {}
    }

    // First register the default implementation / 'base class' for IFoo.
    container.transient(IFoo, []);

    container.singleton(MyService, [IFoo]); // MyService depends on IFoo

    container.transient(ConcreteFoo, []); // Register an override service
    container.override(IFoo, ConcreteFoo); // ConcreteFoo will be passed to MyService
  • Retrieves a service from the container, and resolves it's dependencies.

    Will throw a ServiceNotFoundError if the service was not registered via register.

    Type Parameters

    • T

    Parameters

    Returns T

    Example

    const instance = container.get(MyClass);
    
  • Resolves an instance of the given class.

    Type Parameters

    • T

    Parameters

    Returns undefined | T

  • Creates a new instance of a service and resolves/injects it's dependencies.

    Type Parameters

    • T

    Parameters

    Returns T

Generated using TypeDoc