simple_injection package

Submodules

simple_injection.service_collection module

class simple_injection.service_collection.ServiceCollection[source]

Bases: object

add(service_to_add: Type[T], service_implementation: Union[T, Type[T]], service_lifetime: simple_injection.service_collection.ServiceLifetime, args: Optional[List[Any]] = None) → None[source]

Add a service to the service collection.

If adding a service that has already been added. The new service will
override the old service. It will also keep track of the old and new service/s so they can all injected when requesting a List[ServiceType] from the ServiceCollection.
Args:

service_to_add (Type[T]): Service to add to the collection. service_implementation (Union[T, Type[T]]): Implementation of the service.

Can either be a class or an instance of a class. If is an instance, then service lifetime must be set to ServiceLifetime.INSTANCE.

service_lifetime (ServiceLifetime): The lifetime policy for the service. args (List[Any], optional): Args to be passed to the class when

instanciating. If None, the args will be deduced by the service collection. Will be used when service_lifetime is not ServiceLifetime.INSTANCE. Defaults to None.
add_instance(service_to_add: Type[T], instance: T) → None[source]
Add an instance service to the service collection. The provided instance will
be used each time the service is requested.
If adding a service that has already been added. The new service will
override the old service. It will also keep track of the old and new service/s so they can all injected when requesting a List[ServiceType] from the ServiceCollection.
Args:
service_to_add (Type[T]): Service to add to the collection. instance (T): Instance for the service.
add_singleton(service_to_add: Type[T], service_implementation: Optional[Type[T]] = None, args: Optional[List[Any]] = None) → None[source]
Add a singleton service to the service collection. A singleton service
will share the same instance for request from the collection, and will be created the first time it is requested.
If adding a service that has already been added. The new service will
override the old service. It will also keep track of the old and new service/s so they can all injected when requesting a List[ServiceType] from the ServiceCollection.
Args:

service_to_add (Type[T]): Service to add to the collection. service_implementation (Type[T], optional): Implementation of the service.

If None, it will use service_to_add. Deafaults None.
args (List[Any], optional): Args to be passed to the class when
instanciating. If None, the args will be deduced by the service collection. Defaults to None.
add_transient(service_to_add: Type[T], service_implementation: Optional[Type[T]] = None, args: Optional[List[Any]] = None) → None[source]
Add a transient service to the service collection. A transient service
will have a new instance created everytime the service is requested from the collection.
If adding a service that has already been added. The new service will
override the old service. It will also keep track of the old and new service/s so they can all injected when requesting a List[ServiceType] from the ServiceCollection.
Args:

service_to_add (Type[T]): Service to add to the collection. service_implementation (Type[T], optional): Implementation of the service.

If None, it will use service_to_add. Deafaults None.
args (List[Any], optional): Args to be passed to the class when
instanciating. If None, the args will be deduced by the service collection. Defaults to None.
call_function(function: Callable[[], T]) → T[source]

Call a function resolving inputs using the service collection.

Args:
function (Callable): The function to call.
Returns:
Any: The return value from the function.
resolve(service_to_resolve: Type[T]) → T[source]
Resolve a service declared in the container. The service must be
declared in one of the add methods for it to be resolved.
Args:
service_to_resolve (Type[T]): Service to resolve.
Returns:
T: An instance of the resolved service.
class simple_injection.service_collection.ServiceLifetime[source]

Bases: enum.Enum

Service lifetimes. There are currently 3 different service lifetimes.

TRANSIENT: A new instance will be created every time the service is requested. SINGLETON: The same instance is used across all requests. Instance is created

the first time the service is requested.

INSTANCE: The provided instance will be used for all requests.

INSTANCE = 3
SINGLETON = 2
TRANSIENT = 1
exception simple_injection.service_collection.ServiceResolutionError(message, errors, service)[source]

Bases: Exception

class simple_injection.service_collection.ServiceResolverFlags[source]

Bases: enum.Enum

Flag to use when using args when adding a service.

REQUIRED_SERVICE: Use this flag in the args
and when resolving the service, it will look up the service in the service collection where the flag was used.
Example:
class Service:
pass
class Example:
def __init__(self, i: int, service: Service):
self._i = i self._service = service

collection = ServiceCollection() collection.add_transient(Service)

# This would be the same as Example(1, Service()) collection.add_transient(Example, args=[1, ServiceResolverFlag.REQUIRED_SERVICE])

REQUIRED_SERVICE = 1

Module contents