Container class

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 [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/config.iconfigurable.html IConfigurable interface] and passes them their configuration parameters
  • Sets references to components that implement [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/refer.ireferenceable.html IReferenceable interface] and passes them references of all components in the container
  • Opens components that implement [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/run.iopenable.html IOpenable interface]

On container stop actions are performed in reversed order:

  • Closes components that implement [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/run.iclosable.html ICloseable interface]
  • Unsets references in components that implement [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/refer.iunreferenceable.html IUnreferenceable interface]
  • Destroys components in the container.

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 [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/config.iconfigurable.html IConfigurable] (in the PipServices 'Commons' package) See [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/refer.ireferenceable.html IReferenceable] (in the PipServices 'Commons' package) See [https://rawgit.com/pip-services-node/pip-services3-commons-node/master/doc/api/interfaces/run.iopenable.html 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
============================

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

var 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');
    });
});
Implementers

Constructors

Container([String name String description ])
Creates a new instance of the container. [...]

Properties

config ContainerConfig
read / write
factories DefaultContainerFactory
read / write
info ↔ ContextInfo
read / write
logger ↔ ILogger
read / write
references ContainerReferences
read / write
hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

addFactory(IFactory factory) → void
Adds a factory to the container. The factory is used to create components added to the container by their locators (descriptors). [...]
close(String correlationId) → Future
Closes component and frees used resources. [...]
configure(ConfigParams config) → void
Configures component by passing configuration parameters. [...]
isOpen() → bool
Checks if the component is opened. [...]
open(String correlationId) → Future
Opens the component. [...]
readConfigFromFile(String correlationId, String path, ConfigParams parameters) → void
Reads container configuration from JSON or YAML file and parameterizes it with given values. [...]
setReferences(IReferences references) → void
Sets references to dependent components. [...]
unsetReferences() → void
Unsets (clears) previously set references to dependent components.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator. [...]
inherited