Class Linker<K, V>

The Singleton Linker class provides the means to lookup and retrieve instances of Targeted constructors. The Linker is used throughout the SGRUD client libraries, e.g., by the Factor decorator, to provide and retrieve different centrally provisioned class instances.

Decorator

Singleton

Example

Preemptively link an instance:

import { Linker } from '@sgrud/core';
import { Service } from './service';

new Linker<typeof Service>([
[Service, new Service('linked')]
]);

Type Parameters

Hierarchy

Constructors

Methods

Constructors

  • Type Parameters

    Parameters

    • Optional entries: null | readonly (readonly [K, V])[]

    Returns Linker<K, V>

  • Type Parameters

    Parameters

    • Optional iterable: null | Iterable<readonly [K, V]>

    Returns Linker<K, V>

Methods

  • Overridden get method. Calling this method looks up the linked instance based on the supplied target constructor. If no linked instance is found, one is created by calling the new operator on the target constructor. Therefor the target constructors must not require parameters.

    Parameters

    • target: K

      The target constructor for which to retrieve an instance.

    Returns V

    The already linked or a newly constructed and linked instance.

    Example

    Retrieve a linked instance:

    import { Linker } from '@sgrud/core';
    import { Service } from './service';

    new Linker<typeof Service>().get(Service);
  • The getAll method returns all linked instances, which satisfy instanceof target. Use this method when multiple linked target constructors extend the same base class and are to be retrieved.

    Parameters

    • target: K

      The target constructor for which to retrieve instances.

    Returns V[]

    All already linked instance of the target constructor.

    Example

    Retrieve all linked instances of a Service:

    import { Linker } from '@sgrud/core';
    import { Service } from './service';

    new Linker<typeof Service>().getAll(Service);

Generated using TypeDoc