Container

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

Implements: IConfigurable interface, IReferenceable interface, IUnreferenceable interface, IOpenable interface

Description

The Container class allows you to create an inversion of control (IoC) container that creates components and manages their lifecycle.

Important points

  • The container is driven by configuration, which is usually stored in a JSON or YAML file.
  • The configuration contains a list of components identified by their type or locator, followed by their configuration.
  • On start, a container performs the following actions:
    • Creates components using their types or calls registered factories to create them 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 stop, a container reverses the orden of its actions:

Configuration parameters

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

Constructors

Creates a new instance of the container.

Container([String? name, String? description])

  • name: String? - (optional) container’s name (accessible via ContextInfo)
  • description: String? - (optional) container’s description (accessible via ContextInfo)

Fields

config

Configuration of the container

config: ContainerConfig?

references

Container’s references

references: ContainerReferences?

logger

Logger.

logger: ILogger

info

Container’s information.

info: ContextInfo?

factories

Default factories.

factories: DefaultContainerFactory

Instance methods

addFactory

Adds a factory to the container. The factory is used to create the components
added to the container by their locators (descriptors).

void addFactory(IFactory factory)

  • factory: IFactory - component factory to be added.

close

Closes the component and frees used resources.

@override

Future close(String? correlationId)

  • correlationId: String? - (optional) transaction id used to trace execution through the call chain.

configure

Configures the component by passing its configuration parameters.

@override

void configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

isOpen

Checks if the component is open.

@override

bool isOpen()

  • returns: bool - true if the component is open and false otherwise.

open

Opens the component.

@override

Future open(String? correlationId)

  • correlationId: String? - (optional) transaction id used to trace execution through the call chain.

readConfigFromFile

Reads the container’s configuration from a JSON or YAML file and parameterizes it with the given values.

Future readConfigFromFile(String? correlationId, String path, ConfigParams parameters)

  • correlationId: String? - (optional) transaction id used to trace execution through the call chain.
  • path: String - path to the configuration file
  • parameters: ConfigParams - configuration parameters or null to skip parameterization.

setReferences

Sets references to dependent components.

@override

void setReferences(IReferences references)

  • references: IReferences - references to locate the component dependencies.

unsetReferences

Unsets (clears) previously set references to dependent components.

@override

void unsetReferences()

Examples

======= config.yaml ========
- 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');
    });
});

See also