Skip to main content

Container

The Container class is a box to hold all registered factories.

In Octo, we minimize the use of the new keyword and get instance of a class using its factory. The factory has full control over how the instance is created.

By using the Container and Factory concepts, it is possible to override internal class definitions at runtime. It is an incredibly powerful tool to customize implementation.

@group

Functions/Container

Index

Methods

get

  • get<T, F>(type: string | Constructable<T>, options?: { args?: Parameters<F[create]>; metadata?: {} }): Promise<T>
  • To get an instance of a class from its factory. If the factory is not registered yet, this waits for it, and times out - see Container.setFactoryTimeout.


    Type parameters

    • T
    • F: Factory<T> = never

    Parameters

    • type: string | Constructable<T>

      The type or name of the class to get.

    • optionaloptions: { args?: Parameters<F[create]>; metadata?: {} }

      Allows selection of a specific factory to use to get the instance.

      • The args option supplies arguments to the factory.
      • The metadata option identifies the factory.
      • optionalargs: Parameters<F[create]>
      • optionalmetadata: {}

    Returns Promise<T>

    The instance of the class.

has

  • has<T>(type: string | Constructable<T>, options?: { metadata?: {} }): boolean
  • Whether a factory is registered for the class.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>
    • optionaloptions: { metadata?: {} }
      • optionalmetadata: {}

    Returns boolean

registerFactory

  • registerFactory<T>(type: string | Constructable<T>, factory: Factory<T>, options?: { metadata?: {} }): void
  • To register a factory for a class. Prefer the Factory decorator; call this directly only when the factory is built at runtime.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

      The type or name of the class for which the factory is registered.

    • factory: Factory<T>

      The factory class being registered.

    • optionaloptions: { metadata?: {} }

      Distinguishes between different factories of the same class.

      • The metadata attaches custom metadata to the factory.
      • optionalmetadata: {}

    Returns void

registerValue

  • registerValue<T>(type: string | Constructable<T>, value: T, options?: { metadata?: {} }): void
  • To register a ready-made instance, instead of a factory that builds one.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>
    • value: T
    • optionaloptions: { metadata?: {} }
      • optionalmetadata: {}

    Returns void

replaceValue

  • replaceValue<T>(type: string | Constructable<T>, value: T, options?: { metadata?: {} }): void
  • To register a ready-made instance over whatever is already registered under the same type and metadata, rather than refusing as Container.registerValue does.

    Registering once is the right default - two factories for one type is usually a mistake. This is for the callers that legitimately register the same thing more than once: a module whose onInit runs on every compose(), and a test replacing a factory with a mock.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>
    • value: T
    • optionaloptions: { metadata?: {} }
      • optionalmetadata: {}

    Returns void

reset

  • reset(): void
  • To clear every registered factory and empty the container. Mostly used in testing.


    Returns void

setFactoryTimeout

  • setFactoryTimeout(timeoutInMs: number): void
  • To change how long get() waits for a factory to be registered.

    @defaultValue

    3000


    Parameters

    • timeoutInMs: number

      The timeout in milliseconds.

    Returns void

unRegisterFactory

  • unRegisterFactory<T>(type: string | Constructable<T>, options?: { metadata?: {} }): void
  • To unregister a class's factories, so that your own can take their place.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

      The type or name of the class whose factories are unregistered.

    • optionaloptions: { metadata?: {} }

      Pass metadata to unregister one specific factory. Without it, all of the class's factories are unregistered.

      • optionalmetadata: {}

    Returns void

staticgetInstance

  • The Container is a singleton - this is how you reach it.


    Returns Container