Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Container

Inversion of control (IoC) container that creates components and manages their lifecycle.

The container is driven by configuration, that usually stored in JSON or YAML file. The configuration contains a list of components identified by type or locator, followed by component configuration.

On container start it performs the following actions:

  • Creates components using their types or calls registered factories to create components using their locators
  • Configures components that implement IConfigurable interface and passes them their configuration parameters
  • Sets references to components that implement IReferenceable interface and passes them references of all components in the container
  • Opens components that implement IOpenable interface

On container stop actions are performed in reversed order:

The component configuration can be parameterized by dynamic values. That allows specialized containers to inject parameters from command line or from environment variables.

The container automatically creates a ContextInfo component that carries detail information about the container and makes it available for other components.

see

IConfigurable (in the PipServices "Commons" package)

see

IReferenceable (in the PipServices "Commons" package)

see

IOpenable (in the PipServices "Commons" package)

Configuration parameters

  • name: the context (container or process) name
  • description: human-readable description of the context
  • properties: entire section of additional descriptive properties
     - ...
    

Example

======= config.yml ========
- descriptor: mygroup:mycomponent1:default:default:1.0
  param1: 123
  param2: ABC

- type: mycomponent2,mypackage
  param1: 321
  param2: XYZ
============================

let container = new Container();
container.addFactory(new MyComponentFactory());

let parameters = ConfigParams.fromValue(process.env);
container.readConfigFromFile("123", "./config/config.yml", parameters);

container.open("123", (err) => {
    console.log("Container is opened");
    ...
    container.close("123", (err) => {
        console.log("Container is closed");
    });
});

Hierarchy

Implements

  • any
  • any
  • any
  • any

Index

Constructors

constructor

  • new Container(name?: string, description?: string): Container
  • Creates a new instance of the container.

    Parameters

    • Optional name: string

      (optional) a container name (accessible via ContextInfo)

    • Optional description: string

      (optional) a container description (accessible via ContextInfo)

    Returns Container

Properties

Protected _config

Protected _factories

_factories: DefaultContainerFactory = new DefaultContainerFactory()

Protected _info

_info: ContextInfo

Protected _logger

_logger: ILogger = new NullLogger()

Protected _references

_references: ContainerReferences

Methods

addFactory

  • addFactory(factory: IFactory): void
  • Adds a factory to the container. The factory is used to create components added to the container by their locators (descriptors).

    Parameters

    • factory: IFactory

      a component factory to be added.

    Returns void

close

  • close(correlationId: string, callback?: function): void
  • Closes component and frees used resources.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

configure

  • configure(config: ConfigParams): void
  • Configures component by passing configuration parameters.

    Parameters

    • config: ConfigParams

      configuration parameters to be set.

    Returns void

isOpen

  • isOpen(): boolean
  • Checks if the component is opened.

    Returns boolean

    true if the component has been opened and false otherwise.

open

  • open(correlationId: string, callback?: function): void
  • Opens the component.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

readConfigFromFile

  • readConfigFromFile(correlationId: string, path: string, parameters: ConfigParams): void
  • Reads container configuration from JSON or YAML file and parameterizes it with given values.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • path: string

      a path to configuration file

    • parameters: ConfigParams

      values to parameters the configuration or null to skip parameterization.

    Returns void

setReferences

  • setReferences(references: IReferences): void
  • Sets references to dependent components.

    Parameters

    • references: IReferences

      references to locate the component dependencies.

    Returns void

unsetReferences

  • unsetReferences(): void
  • Unsets (clears) previously set references to dependent components.

    Returns void

Generated using TypeDoc