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(name: str = None, description: str = None)

  • name: str - (optional) container’s name (accessible via ContextInfo)
  • description: str - (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

add_factory

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

add_factory(factory: IFactory)

  • factory: IFactory - component factory to be added.

close

Closes the component and frees used resources.

close(correlation_id: Optional[str])

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through the call chain.

configure

Configures the component by passing its configuration parameters.

configure(config: ConfigParams)

  • config: ConfigParams - configuration parameters to be set.

is_open

Checks if the component is open.

is_open(): bool

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

open

Opens the component.

open(correlation_id: Optional[str])

  • correlation_id: Optional[str] - (optional) transaction id to trace execution through call chain.

read_config_from_file

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

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

  • correlation_id: Optional[str] - (optional) transaction id used to trace execution through the call chain.
  • path: str - path to the configuration file
  • parameters: ConfigParams - configuration parameters or None to skip parameterization.

set_references

Sets references to dependent components.

set_references(references: IReferences)

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

unset_referencesf

Unsets (clears) previously set references to dependent components.

unset_referencesf()

Examples

======= 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.environ)
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"

See also