Skip to main content

Factory

Callable

  • Factory<T>(type: string | Constructable<T>, options?: { metadata?: {} }): (constructor: { create: (...args: unknown[]) => Promise<T> }) => void

  • A @Factory is a class decorator to be placed on top of a class that represents a factory. A factory is responsible for creating instances of a class - Octo resolves every service through one rather than calling new, which is what lets you swap an internal implementation for your own.

    The same class may have several factories, told apart by their metadata. The factory registered without metadata is the default.

    @example
    // Factory definition.
    @Factory<MyService>(MyService, { metadata: { key: 'value' } })
    export class MyServiceFactory {
    static async create(arg1, arg2): Promise<MyService> { ... }
    }

    const container = Container.getInstance();

    // Get instance using default factory.
    const myService = await container.get(MyService);

    // Get instance using specific factory.
    const myService = await container.get(MyService, { metadata: { key: 'value' } });

    // Pass args to factory.
    const myService = await container.get(MyService, { args: [arg1, arg2] });
    @group

    Decorators

    @remarks

    A factory that produces a singleton must await its own dependencies before the instance check. Awaiting inside the check is a race - concurrent callers each pass the check while suspended, and each gets a different instance.

    static async create(): Promise<MyService> {
    const otherInstance = await Container.get(OtherService); // Await first.
    if (!this.instance) {
    this.instance = new MyService(otherInstance);
    }
    return this.instance;
    }

    Type parameters

    • T

    Parameters

    • type: string | Constructable<T>

      The type of class that the factory creates.

    • optionaloptions: { metadata?: {} }

      Allows registering multiple factories for the same class that differ by metadata.

      • optionalmetadata: {}

    Returns (constructor: { create: (...args: unknown[]) => Promise<T> }) => void

    The decorated class.

      • (constructor: { create: (...args: unknown[]) => Promise<T> }): void
      • Parameters

        • constructor: { create: (...args: unknown[]) => Promise<T> }
          • create: (...args: unknown[]) => Promise<T>

        Returns void