From 1c94fb5bd14ab138c81727e75fb910b5a3ab90f9 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 24 May 2022 17:51:17 +0200 Subject: [PATCH] Added logic to load singletons when requested not when registered --- .../service_collection.py | 35 +++++++------------ .../custom/general/src/general/application.py | 5 +++ .../general/src/general/parameter_startup.py | 4 +-- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/cpl_core/dependency_injection/service_collection.py b/src/cpl_core/dependency_injection/service_collection.py index 1b428a8c..a608c710 100644 --- a/src/cpl_core/dependency_injection/service_collection.py +++ b/src/cpl_core/dependency_injection/service_collection.py @@ -1,5 +1,7 @@ from typing import Union, Type, Callable, Optional +import lifetime as lifetime + from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.database.database_settings import DatabaseSettings from cpl_core.database.context.database_context_abc import DatabaseContextABC @@ -37,6 +39,14 @@ class ServiceCollection(ServiceCollectionABC): self._service_descriptors.append(ServiceDescriptor(service, lifetime)) + def _add_descriptor_by_lifetime(self, service_type: Type, lifetime: ServiceLifetimeEnum, service: Callable = None): + if service is not None: + self._add_descriptor(service, lifetime) + else: + self._add_descriptor(service_type, lifetime) + + return self + def add_db_context(self, db_context_type: Type[DatabaseContextABC], db_settings: DatabaseSettings): self.add_singleton(DatabaseContextABC, db_context_type) self._database_context = self.build_service_provider().get_service(DatabaseContextABC) @@ -46,34 +56,15 @@ class ServiceCollection(ServiceCollectionABC): self.add_singleton(LoggerABC, Logger) def add_singleton(self, service_type: Union[type, object], service: Union[type, object] = None): - impl = None - if service is not None: - if isinstance(service, type): - impl = self.build_service_provider().build_service(service) - - self._add_descriptor(impl, ServiceLifetimeEnum.singleton) - else: - if isinstance(service_type, type): - impl = self.build_service_provider().build_service(service_type) - - self._add_descriptor(impl, ServiceLifetimeEnum.singleton) - + self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.singleton, service) 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) - + self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.scoped, service) return self def add_transient(self, service_type: type, service: type = None): - if service is not None: - self._add_descriptor(service, ServiceLifetimeEnum.transient) - else: - self._add_descriptor(service_type, ServiceLifetimeEnum.transient) - + self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.transient, service) return self def build_service_provider(self) -> ServiceProviderABC: diff --git a/src/tests/custom/general/src/general/application.py b/src/tests/custom/general/src/general/application.py index c3113fb8..8f282560 100644 --- a/src/tests/custom/general/src/general/application.py +++ b/src/tests/custom/general/src/general/application.py @@ -7,6 +7,7 @@ from cpl_core.console.console import Console from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.logging.logger_abc import LoggerABC from cpl_core.mailing import EMailClientABC, EMail +from cpl_core.pipes import IPAddressPipe from test_service import TestService @@ -43,5 +44,9 @@ class Application(ApplicationABC): self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}') Console.spinner('Test', self._wait, 2, spinner_foreground_color='red') test: TestService = self._services.get_service(TestService) + ip_pipe: IPAddressPipe = self._services.get_service(IPAddressPipe) test.run() + test2: TestService = self._services.get_service(TestService) + ip_pipe2: IPAddressPipe = self._services.get_service(IPAddressPipe) + Console.write_line(f'DI working: {test == test2 and ip_pipe != ip_pipe2}') # self.test_send_mail() diff --git a/src/tests/custom/general/src/general/parameter_startup.py b/src/tests/custom/general/src/general/parameter_startup.py index 81380936..a8a7d08e 100644 --- a/src/tests/custom/general/src/general/parameter_startup.py +++ b/src/tests/custom/general/src/general/parameter_startup.py @@ -1,9 +1,9 @@ +from arguments.generate_argument import GenerateArgument +from arguments.install_argument import InstallArgument from cpl_core.application import StartupExtensionABC from cpl_core.configuration import ConfigurationABC, ArgumentTypeEnum from cpl_core.dependency_injection import ServiceCollectionABC from cpl_core.environment import ApplicationEnvironmentABC -from arguments.generate_argument import GenerateArgument -from arguments.install_argument import InstallArgument class ParameterStartup(StartupExtensionABC):