pip_services3_container.Container module

pip_services3_container.Container

Container implementation.

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_container.Container.Container(name: str = None, description: str = None)

Bases: pip_services3_commons.config.IConfigurable.IConfigurable, pip_services3_commons.refer.IReferenceable.IReferenceable, pip_services3_commons.refer.IUnreferenceable.IUnreferenceable, pip_services3_commons.run.IOpenable.IOpenable

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:
  • Closes components that implement ICloseable interface

  • Unsets references in components that implement 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.

### 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.yaml ========
- descriptor: mygroup:mycomponent1:default:default:1.0
param1: 123
param2: ABC

- type: mycomponent2,mypackage
param1: 321
param2: XYZ
============================
container = Container()
container.add_factory(MyComponentFactory())

parameters = ConfigParams.from_value(os.env)
container.read_config_from_file("123", "./config/config.yml", parameters)

container.open("123")
print "Container is opened"
# process...
container.close("123")
print "Container is closed"
add_factory(factory: pip_services3_components.build.IFactory.IFactory)

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

Parameters

factory – a component factory to be added.

close(correlation_id: Optional[str])

Closes component and frees used resources.

Parameters

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

configure(config: pip_services3_commons.config.ConfigParams.ConfigParams)

Configures component by passing configuration parameters.

Parameters

config – configuration parameters to be set.

is_open()bool

Checks if the component is opened.

Returns

true if the component has been opened and false otherwise.

open(correlation_id: Optional[str])

Opens the component.

Parameters

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

read_config_from_file(correlation_id: Optional[str], path: str, parameters: pip_services3_commons.config.ConfigParams.ConfigParams)

Reads container configuration from JSON or YAML file and parameterizes it with given values.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • path – a path to configuration file

  • parameters – values to parameters the configuration or null to skip parameterization.

set_references(references: pip_services3_commons.refer.IReferences.IReferences)

Sets references to dependent components.

Parameters

references – references to locate the component dependencies.

unset_references()

Unsets (clears) previously set references to dependent components.