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.

Index

Constructors

constructor

Methods

staticget

  • get<T>(type: string | Constructable<T>, options?: { args?: unknown[]; metadata?: {} }): Promise<T>
  • Container.get() allows to get an instance of a class using its factory.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

      The type or name of the class to get.

    • optionaloptions: { args?: unknown[]; 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.

    Returns Promise<T>

    The instance of the class.

staticregisterFactory

  • registerFactory<T>(type: string | Constructable<T>, factory: Factory<T>, options?: { metadata?: {} }): void
  • Container.registerFactory() allows to register a factory for a class.


    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.

    Returns void

staticreset

  • reset(): void
  • Container.reset() clears all registered factories and empties the container. This is mostly used in testing.


    Returns void

staticsetDefault

  • setDefault<T>(type: string | Constructable<T>, factory: Factory<T>): void
  • Container.setDefault() sets a default factory for the class.

    @example
    // Register a factory.
    Container.registerFactory(MyClass, MyClassFactory, { metadata: { key: 'value' } });

    // Without default.
    Container.get(MyClass, { metadata: { key: 'value' } });

    // Set default.
    Container.setDefault(MyClass, MyClassFactory);

    // With default.
    Container.get(MyClass);

    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

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

    • factory: Factory<T>

      The factory class being set as default.

    Returns void

staticsetFactoryTimeout

  • setFactoryTimeout(timeoutInMs: number): void
  • The Container, by default, will wait a maximum of 5 seconds for a factory to resolve and provide an instance. This method allows to change that timeout.


    Parameters

    • timeoutInMs: number

      The timeout in milliseconds.

    Returns void

staticunRegisterFactory

  • Container.unRegisterFactory() will unregister all factories of a class.


    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

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

    Returns void