Skip to content

Initialization

amsdal_glue.init_default_containers

init_default_containers(container=None)

Initializes and registers the default containers for services, managers, and executors.

This function sets up the default dependency injection containers by registering various services, managers, planners, and executors. It ensures that the application has the necessary components to function correctly.

Registers
Example

Before using the application, you need to register all services, managers, planners, and executors. You can do this by calling the init_default_containers function that sets up the default containers. Or you can register them manually via Container class.

Here is an example of how to use the init_default_containers function:

from amsdal_glue import init_default_containers

init_default_containers()

amsdal_glue.Container

The Container class is a central registry for managing dependencies in the application. It provides separate containers for different types of dependencies, such as managers, services, executors, and planners.

Instead of directly instantiating dependencies, use the Container class to retrieve instances of the required dependencies. This promotes a more modular design, allows for easier management of dependencies and simplifies testing.

Example

These examples demonstrate how to work with the Container class. Note that the examples assume usage via the amsdal_glue package. If you are using the Container class directly via the amsdal_glue_core package, you should adjust the imports accordingly.

  1. Get an instance of the ConnectionManager and register connection pool

    import amsdal_glue
    from amsdal_glue import Container
    
    connection_manager = Container.managers.get(ConnectionManager)
    connection_manager.register_connection_pool(
        amsdal_glue.DefaultConnectionPool(
            amsdal_glue.PostgresConnection,
            dsn='postgres://db_user:db_password@localhost:5433/db_name',
        ),
    )
    

  2. Register the own SequentialExecutor implementation

    import amsdal_glue
    from amsdal_glue import Container
    from amsdal_glue.executors import SequentialExecutor
    
    
    class MySequentialExecutor(SequentialExecutor):
        pass
    
    
    Container.managers.register(SequentialExecutor, MySequentialExecutor)
    

Note, when you get an instance of the dependency, you should use the base class type, not the implementation type.

Attributes:

Name Type Description
managers DependencyContainer

A container for manager dependencies.

services DependencyContainer

A container for service dependencies.

executors DependencyContainer

A container for executor dependencies.

planners DependencyContainer

A container for planner dependencies.

amsdal_glue_core.containers.DependencyContainer

The DependencyContainer class is responsible for managing the registration and retrieval of dependencies. It maintains a mapping of dependency types to their respective provider types.

Attributes:

Name Type Description
_providers dict[type[Any], type[Any]]

A dictionary mapping dependency types to provider types.

Methods:

Name Description
register

type[Any], provider: type[Any]) -> None: Registers a provider for a given dependency type.

get

type[Any]) -> Any: Retrieves an instance of the provider for the given dependency type.

register

register(dependency, provider)

Registers a provider for a given dependency type.

Parameters:

Name Type Description Default
dependency type[Any]

The type of the dependency.

required
provider type[Any]

The type of the provider.

required

get

get(dependency)

Retrieves an instance of the provider for the given dependency type.

Parameters:

Name Type Description Default
dependency type[Any]

The type of the dependency.

required

Returns:

Name Type Description
Any T

An instance of the provider for the given dependency type.

Raises:

Type Description
ValueError

If no provider is registered for the given dependency type.

get_dependency_type

get_dependency_type(instance)

Retrieves the type of the dependency for the given instance.

Parameters:

Name Type Description Default
instance Any

An instance of the provider.

required

Returns:

Type Description
type[Any]

type[Any]: The type of the dependency for the given instance.

Raises:

Type Description
ValueError

If no dependency type is registered for the given instance.