Function Factor

  • Prototype property decorator factory. Applying this decorator replaces the decorated prototype property with a getter, which returns the linked instance of a Targeted constructor, referenced by the targetFactory. Depending on the supplied transient value, the target constructor is invoked to construct (and link) an instance, if none is linked beforehand.

    Type Parameters

    • K extends (new () => any)

      The Targeted constructor type.

    Parameters

    • targetFactory: (() => K)

      A forward reference to the target constructor.

        • (): K
        • Returns K

    • transient: boolean = false

      Whether an instance is constructed if none is linked.

    Returns ((prototype, propertyKey) => void)

    A prototype property decorator.

      • (prototype, propertyKey): void
      • Parameters

        • prototype: object
        • propertyKey: PropertyKey

        Returns void

    Example

    Factor an eager and lazy service:

    import { Factor } from '@sgrud/core';
    import { EagerService, LazyService } from './services';

    export class ServiceHandler {

    ⁠@Factor(() => EagerService)
    private readonly service!: EagerService;

    ⁠@Factor(() => LazyService, true)
    private readonly service?: LazyService;

    }

    See

Generated using TypeDoc