From 773b1543718fe94eac97be34d5b4c4a1807f2e29 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 18 May 2022 18:20:25 +0200 Subject: [PATCH] Improved service collection --- .../dependency_injection/service_collection.py | 8 ++++++-- .../service_collection_abc.py | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/cpl_core/dependency_injection/service_collection.py b/src/cpl_core/dependency_injection/service_collection.py index 337f172a..1b428a8c 100644 --- a/src/cpl_core/dependency_injection/service_collection.py +++ b/src/cpl_core/dependency_injection/service_collection.py @@ -10,7 +10,6 @@ from cpl_core.dependency_injection.service_lifetime_enum import ServiceLifetimeE from cpl_core.dependency_injection.service_provider import ServiceProvider from cpl_core.logging.logger_service import Logger from cpl_core.logging.logger_abc import LoggerABC -from cpl_core.utils.credential_manager import CredentialManager class ServiceCollection(ServiceCollectionABC): @@ -59,12 +58,15 @@ class ServiceCollection(ServiceCollectionABC): self._add_descriptor(impl, ServiceLifetimeEnum.singleton) + return self + def add_scoped(self, service_type: Type, service: Callable = None): if service is not None: self._add_descriptor(service, ServiceLifetimeEnum.scoped) else: self._add_descriptor(service_type, ServiceLifetimeEnum.scoped) - + + return self def add_transient(self, service_type: type, service: type = None): if service is not None: @@ -72,5 +74,7 @@ class ServiceCollection(ServiceCollectionABC): else: self._add_descriptor(service_type, ServiceLifetimeEnum.transient) + return self + def build_service_provider(self) -> ServiceProviderABC: return ServiceProvider(self._service_descriptors, self._configuration, self._database_context) diff --git a/src/cpl_core/dependency_injection/service_collection_abc.py b/src/cpl_core/dependency_injection/service_collection_abc.py index 2f69691a..13ddca96 100644 --- a/src/cpl_core/dependency_injection/service_collection_abc.py +++ b/src/cpl_core/dependency_injection/service_collection_abc.py @@ -31,7 +31,7 @@ class ServiceCollectionABC(ABC): pass @abstractmethod - def add_transient(self, service_type: Type, service: Callable = None): + def add_transient(self, service_type: Type, service: Callable = None) -> 'ServiceCollectionABC': r"""Adds a service with transient lifetime Parameter @@ -40,11 +40,15 @@ class ServiceCollectionABC(ABC): Type of the service service: :class:`Callable` Object of the service + + Returns + ------ + self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC """ pass @abstractmethod - def add_scoped(self, service_type: Type, service: Callable = None): + def add_scoped(self, service_type: Type, service: Callable = None) -> 'ServiceCollectionABC': r"""Adds a service with scoped lifetime Parameter @@ -53,11 +57,15 @@ class ServiceCollectionABC(ABC): Type of the service service: :class:`Callable` Object of the service + + Returns + ------ + self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC """ pass @abstractmethod - def add_singleton(self, service_type: Type, service: Callable = None): + def add_singleton(self, service_type: Type, service: Callable = None) -> 'ServiceCollectionABC': r"""Adds a service with singleton lifetime Parameter @@ -66,6 +74,10 @@ class ServiceCollectionABC(ABC): Type of the service service: :class:`Callable` Object of the service + + Returns + ------ + self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC """ pass