Container

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

Inherits: IConfigurable, IReferenceable, IUnreferenceable, IOpenable

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 the IConfigurable interface and passes them their configuration parameters.
    • Sets references to components that implement the IReferenceable interface and passes them references of all components in the container.
    • Opens components that implement the IOpenable interface.
  • On stop, a container reverses the orden of its actions:
    • Closes components that implement the IClosable interface.
    • Unsets references in components that implement the IUnreferenceable interface.
    • Destroys components in the container.

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.

public Container(string name = null, string description = null)

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

Fields

_config

Configuration of the container

protected _config: ContainerConfig

_references

Container’s references

protected _references: ContainerReferences

_logger

Logger.

protected _logger: ILogger

_info

Container’s information.

protected _info: ContextInfo

_factories

Default factories.

protected _factories: DefaultContainerFactory

Instance methods

AddFactory

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

public void AddFactory(IFactory factory)

  • factory: IFactory - component factory to be added.

Close

Closes the component and frees used resources.

public Task CloseAsync(string correlationId)

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

Configure

Configures the component by passing its configuration parameters.

public virtual void Configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

IsOpen

Checks if the component is open.

public virtual bool IsOpen()

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

Open

Opens the component.

public Task OpenAsync(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.

public void 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.

public virtual void SetReferences(IReferences references)

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

UnsetReferences

Unsets (clears) previously set references to dependent components.

public virtual 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");
Console.Out.WriteLine("Container is opened");
...
container.Close("123");
Console.Out.WriteLine("Container is closed");

See also