Improved service collection

This commit is contained in:
Sven Heidemann 2022-05-18 18:20:25 +02:00
parent 5b59d1d384
commit 773b154371
2 changed files with 21 additions and 5 deletions

View File

@ -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.dependency_injection.service_provider import ServiceProvider
from cpl_core.logging.logger_service import Logger from cpl_core.logging.logger_service import Logger
from cpl_core.logging.logger_abc import LoggerABC from cpl_core.logging.logger_abc import LoggerABC
from cpl_core.utils.credential_manager import CredentialManager
class ServiceCollection(ServiceCollectionABC): class ServiceCollection(ServiceCollectionABC):
@ -59,12 +58,15 @@ class ServiceCollection(ServiceCollectionABC):
self._add_descriptor(impl, ServiceLifetimeEnum.singleton) self._add_descriptor(impl, ServiceLifetimeEnum.singleton)
return self
def add_scoped(self, service_type: Type, service: Callable = None): def add_scoped(self, service_type: Type, service: Callable = None):
if service is not None: if service is not None:
self._add_descriptor(service, ServiceLifetimeEnum.scoped) self._add_descriptor(service, ServiceLifetimeEnum.scoped)
else: else:
self._add_descriptor(service_type, ServiceLifetimeEnum.scoped) self._add_descriptor(service_type, ServiceLifetimeEnum.scoped)
return self
def add_transient(self, service_type: type, service: type = None): def add_transient(self, service_type: type, service: type = None):
if service is not None: if service is not None:
@ -72,5 +74,7 @@ class ServiceCollection(ServiceCollectionABC):
else: else:
self._add_descriptor(service_type, ServiceLifetimeEnum.transient) self._add_descriptor(service_type, ServiceLifetimeEnum.transient)
return self
def build_service_provider(self) -> ServiceProviderABC: def build_service_provider(self) -> ServiceProviderABC:
return ServiceProvider(self._service_descriptors, self._configuration, self._database_context) return ServiceProvider(self._service_descriptors, self._configuration, self._database_context)

View File

@ -31,7 +31,7 @@ class ServiceCollectionABC(ABC):
pass pass
@abstractmethod @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 r"""Adds a service with transient lifetime
Parameter Parameter
@ -40,11 +40,15 @@ class ServiceCollectionABC(ABC):
Type of the service Type of the service
service: :class:`Callable` service: :class:`Callable`
Object of the service Object of the service
Returns
------
self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC
""" """
pass pass
@abstractmethod @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 r"""Adds a service with scoped lifetime
Parameter Parameter
@ -53,11 +57,15 @@ class ServiceCollectionABC(ABC):
Type of the service Type of the service
service: :class:`Callable` service: :class:`Callable`
Object of the service Object of the service
Returns
------
self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC
""" """
pass pass
@abstractmethod @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 r"""Adds a service with singleton lifetime
Parameter Parameter
@ -66,6 +74,10 @@ class ServiceCollectionABC(ABC):
Type of the service Type of the service
service: :class:`Callable` service: :class:`Callable`
Object of the service Object of the service
Returns
------
self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC
""" """
pass pass