Dependency injection container that can create objects marked with the injectable and automatically resolving their dependencies.

To use the container mark any class with the injectable-decorator. injectable allows specifying the lifecycle policy of the object which determines if the container will create a new instance or use the existing instance of the class registered in the container.

The container resolves all undefined constructor parameters and all properties marked with injectable.property.

Circular references are supported and handled by using a lzy resolution proxy.

Usage:

@injectable()
class Bar {
constructor(private foo: Foo) {
}

public helloFooBar() {
this.foo.helloBar();
}

public hello() {
console.log('Hello!');
}
}

@injectable()
class Foo {
constructor(public bar: Bar) {
}

public helloBar() {
this.bar.hello();
}
}

container.get(Foo).helloBar(); // prints 'Hello!'
container.get(Foo).bar.helloFooBar(); // prints 'Hello!'

Hierarchy

  • Container

Constructors

Properties

containerGuid: string = ...
containerPath: string
factories: Map<string, {
    lifecycle?: LifecyclePolicy;
    new: ServiceFactory<object>;
}> = ...

Type declaration

instances: Map<string, any> = ...
logger: Logger = ...
parent?: Container
providers: Map<string, ((receiver) => any)> = ...

Type declaration

    • (receiver): any
    • Parameters

      • receiver: any

      Returns any

resolveStack: string[] = ...
serviceDependencies: Map<string, string[]> = ...
serviceTypes: Map<string, {
    ctor: ServiceCtor<object>;
    options?: ServiceOptions;
}[]> = ...
servicesProvided: symbol = ...
useInstanceProxies: boolean = false

Wrap all instance created by this container in Proxies. When set to true each service instance created by this container is wrapped in a Proxy. Proxied service instances behave the same way as non-proxied service instances but each call will be routed through the proxy which can impact performance.

For now the recommended setting is false unless you need to dynamically swap service instances without recreated the dependencies that use them.

For circular references proxies will always be used even when useInstanceProxies is set to false

root: Container

Global root container; by default all services are contained in there

Accessors

  • get isRoot(): boolean
  • Returns true if this container is the root container of the application. The root container is the container that is created by default and contains all services by default.

    Returns boolean

Methods

  • Create a new instance of a type resolving constructor parameters as dependencies using the container. The returned class is not registered in the container as dependency.

    Type Parameters

    • T extends (new (...args) => any)

    Parameters

    • ctor: T

      Constructor/Type to instantiate

    • Rest ...params: PartialArray<ConstructorParameters<T>, any>

      Constructor params provided

    Returns InstanceType<T>

    New instance of an object who's dependencies are resolved by the container

  • Create a new instance of a type resolving constructor parameters as dependencies using the container and register it as a dependency.

    Type Parameters

    • T extends (new (...args) => any)

    Parameters

    • ctor: T

      Constructor/Type to instantiate

    • Rest ...params: PartialArray<ConstructorParameters<T>, any>

      Constructor params provided

    Returns void

  • Creates a new instance of the specified type resolving dependencies using the current container context. The returned class is not registered in the container as dependency.

    Type Parameters

    • T extends (new (...args) => any)

    Parameters

    • ctor: T

      Constructor type/prototype class definition

    • args: any[] = []

    Returns InstanceType<T>

  • Safe version of resolve that never returns undefined and throws an Error for services that cannot be resolved.

    Type Parameters

    • T extends object

    Parameters

    Returns T

  • Parameters

    • instance: any

    Returns {
        constructor: (new (...args) => any);
    }[]

  • Create a new container that inherits all configuration form the parent but on top of that can register or overwrite dependencies; by default dependency resolution is first attempted using the providers, factories and registered service instance in the new container; if that fails it resolution is delegated to the parent until the root container which will throw an exception in case it cannot resolve the requested module.

    Parameters

    • Optional options: {
          isolated?: boolean;
      }
      • Optional isolated?: boolean

    Returns Container

  • Register an already instantiated service/class in the container, if the instance provides one or more services which are registered using the injectable decorator then those services will be made available in the container.

    Type Parameters

    • T extends object

    Parameters

    • instances: T | T[]
    • Optional options: {
          additionalServiceTypes?: ServiceType<T>[];
      }
      • Optional additionalServiceTypes?: ServiceType<T>[]

        additional service types to register the instance for

    Returns void

  • Register an instance in the container as the specified service service shape

    Type Parameters

    • T extends object

    • I extends object = T

    Parameters

    • instance: I

      Instance providing the service

    • services: ServiceType<T> | ServiceType<T>[]

      Service or array of service shapes provided

    Returns void

    the instance

  • Register a factory that can provide an instance of the specified factory

    Type Parameters

    • T extends object

    • I extends object = T

    Parameters

    Returns void

  • Type Parameters

    • T extends object

    • I extends object = T

    Parameters

    • service: ServiceType<T>
    • provider: ((receiver) => undefined | I | Promise<I>)
        • (receiver): undefined | I | Promise<I>
        • Parameters

          • receiver: any

          Returns undefined | I | Promise<I>

    Returns void

  • Register a service proxy that can be manually set with a concrete instance later on. Any instance that request a service of type T will receive the proxy instance.

    The proxy is not registered as a factory type.

    The proxy instance can be set using the setInstance method on the ProxyTarget returned by this method.

    Type Parameters

    • T extends object

    Parameters

    • type: {
          name: string;
          prototype: T;
      }

      Service type to register

      • name: string
      • prototype: T

    Returns ProxyTarget<T>

    ProxyTarget instance that can be used to set the concrete instance

  • Registers a type in the container as provider of services; type specified is dynamically created when required injecting any resolving any dependency required

    Type Parameters

    • T extends object

    • I extends object = T

    Parameters

    Returns void

  • Drops an active singleton service instance from the container causing subsequent resolutions to create a new instances instead of reusing the existing one.

    Parameters

    • instance: object

      the instance to remove

    Returns void

  • Resolve requested service to an implementation.

    Type Parameters

    • T extends object

    Parameters

    • service: ServiceType<T>

      Service type for which to resolve a concrete class

    • Optional overrideLifecycle: LifecyclePolicy

      Use an alternate lifecycle policy

    • Optional receiver: (new () => object)

      Class ctor for which we are resolving this

        • new (): object
        • Returns object

    • resolver: Container = ...

    Returns undefined | T

  • Resolve injectable parameters for a service using this container.

    Type Parameters

    • T extends (new (...args) => any)

    Parameters

    • ctor: T

      Constructor function

    • args: any[] = []

      arguments

    • Optional instanceGuid: string

      instance guid

    Returns any[]

  • Resolve injectable properties for a service using this container.

    Type Parameters

    • T

    Parameters

    • instance: T

      Instance object

    Returns T

    instance

  • Register a new dependency between two services

    Parameters

    • serviceGuid: string

      the instance guid of the object that owns the dependency

    • dependsOn: object

      instance of the dependency

    Returns void