API

dic.container module

class dic.container.Container(registry_map)

Bases: builtins.object

IoC container.

resolve(component_type, **kwargs)

Resolves an instance of the component type. :param component_type: The type of the component (e.g. a class). :param kwargs: Overriding arguments to use (by name) instead of resolving them. :return: An instance of the component.

class dic.container.ContainerBuilder

Bases: builtins.object

Builds a container from the registered configuration.

build()

Builds a new container using the registered components. :return: A container

register_callback(class_type, callback, component_scope=<class 'dic.scope.InstancePerDependency'>, register_as=None)

Registers the given class for creation via the given callback. :param class_type: The class type. :param callback: The function to call to create/get an instance, of the form fn(component_context) :param component_scope: The scope of the component, defaults to instance per dependency. :param register_as: The types to register the class as, defaults to the given class_type.

register_class(class_type, component_scope=<class 'dic.scope.InstancePerDependency'>, register_as=None)

Registers the given class for creation via its constructor. :param class_type: The class type. :param component_scope: The scope of the component, defaults to instance per dependency. :param register_as: The types to register the class as, defaults to the given class_type.

register_instance(class_type, instance, register_as=None)

Registers the given instance (already created). :param class_type: The class type. :param instance: The instance to register. :param register_as: The types to register the class as, defaults to the given class_type.

register_module(module)

Registers the module instance. :param module: The module to register.

exception dic.container.DependencyResolutionError

Bases: builtins.Exception

class dic.container.Module

Bases: builtins.object

Module to help structure building of the container.

load(builder)

Register dependencies from this module. :param builder: The container builder to register components to.

dic.rel module

class dic.rel.Factory(component_type)

Bases: dic.rel.Relationship

Models a factory relationship capable of creating the given type. The scope of the registered component is respected. Meaning a SingleInstance registration will return the same instance for multiple factory calls.

Overriding arguments can be provided.

resolve(container)
class dic.rel.Lazy(component_type)

Bases: dic.rel.Relationship

Models a lazy relationship. Dependency lookup is delayed until the value is resolved for the first time. Lazy is thread-safe, so only one instance will be resolved if two threads ask at the same time.

resolve(container)
class dic.rel.Relationship

Bases: builtins.object

resolve(container)

Called when the relationship is resolved. E.g. about to be injected.

dic.scope module

class dic.scope.InstancePerDependency

Bases: dic.scope.Scope

Creates an instance per dependency

instance(create_function)
class dic.scope.Scope

Bases: builtins.object

Controls the lifetime scope of a component registration.

instance(create_function)

Gets the instance of the component given its registration. :param create_function: The function to create a new component, if required. :return: The instance

class dic.scope.SingleInstance

Bases: dic.scope.Scope

instance(create_function)

Module contents