Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DependencyResolver

Helper class for resolving component dependencies.

The resolver is configured to resolve named dependencies by specific locator. During deployment the dependency locator can be changed.

This mechanism can be used to clarify specific dependency among several alternatives. Typically components are configured to retrieve the first dependency that matches logical group, type and version. But if container contains more than one instance and resolution has to be specific about those instances, they can be given a unique name and dependency resolvers can be reconfigured to retrieve dependencies by their name.

Configuration parameters

dependencies:

  • [dependency name 1]: Dependency 1 locator (descriptor)
  • ...
  • [dependency name N]: Dependency N locator (descriptor)

References

References must match configured dependencies.

Example

class MyComponent: IConfigurable, IReferenceable {
    private _dependencyResolver: DependencyResolver = new DependencyResolver();
    private _persistence: IMyPersistence;
    ...

    public constructor() {
        this._dependencyResolver.put("persistence", new Descriptor("mygroup", "persistence", "*", "*", "1.0"));
    }

    public configure(config: ConfigParams): void {
        this._dependencyResolver.configure(config);
    }

    public setReferences(references: IReferences): void {
        this._dependencyResolver.setReferences(references);
        this._persistence = this._dependencyResolver.getOneRequired<IMyPersistence>("persistence");
    }
}

// Create mycomponent and set specific dependency out of many
let component = new MyComponent();
component.configure(ConfigParams.fromTuples(
    "dependencies.persistence", "mygroup:persistence:*:persistence2:1.0"
// Override default persistence dependency
));
component.setReferences(References.fromTuples(
    new Descriptor("mygroup","persistence","*","persistence1","1.0"), new MyPersistence(),
    new Descriptor("mygroup","persistence","*","persistence2","1.0"), new MyPersistence()
// This dependency shall be set
));
see

IReferences

Hierarchy

  • DependencyResolver

Implements

Index

Constructors

constructor

Methods

configure

find

  • find<T>(name: string, required: boolean): T[]
  • Finds all matching dependencies by their name.

    throws

    a ReferenceException of required is true and no dependencies found.

    Type parameters

    • T

    Parameters

    • name: string

      the dependency name to locate.

    • required: boolean

      true to raise an exception when no dependencies are found.

    Returns T[]

    a list of found dependencies

getOneOptional

  • getOneOptional<T>(name: string): T
  • Gets one optional dependency by its name.

    Type parameters

    • T

    Parameters

    • name: string

      the dependency name to locate.

    Returns T

    a dependency reference or null of the dependency was not found

getOneRequired

  • getOneRequired<T>(name: string): T
  • Gets one required dependency by its name. At least one dependency must present. If the dependency was found it throws a ReferenceException

    throws

    a ReferenceException if dependency was not found.

    Type parameters

    • T

    Parameters

    • name: string

      the dependency name to locate.

    Returns T

    a dependency reference

getOptional

  • getOptional<T>(name: string): T[]
  • Gets all optional dependencies by their name.

    Type parameters

    • T

    Parameters

    • name: string

      the dependency name to locate.

    Returns T[]

    a list with found dependencies or empty list of no dependencies was found.

getRequired

  • getRequired<T>(name: string): T[]
  • Gets all required dependencies by their name. At least one dependency must be present. If no dependencies was found it throws a ReferenceException

    throws

    a ReferenceException if no dependencies were found.

    Type parameters

    • T

    Parameters

    • name: string

      the dependency name to locate.

    Returns T[]

    a list with found dependencies.

put

  • put(name: string, locator: any): void
  • Adds a new dependency into this resolver.

    Parameters

    • name: string

      the dependency's name.

    • locator: any

      the locator to find the dependency by.

    Returns void

setReferences

  • Sets the component references. References must match configured dependencies.

    Parameters

    Returns void

Static fromTuples

  • Creates a new DependencyResolver from a list of key-value pairs called tuples where key is dependency name and value the depedency locator (descriptor).

    see

    fromTuplesArray

    Parameters

    • Rest ...tuples: any[]

      a list of values where odd elements are dependency name and the following even elements are dependency locator (descriptor)

    Returns DependencyResolver

    a newly created DependencyResolver.

Generated using TypeDoc