Added logic to load singletons when requested not when registered
This commit is contained in:
parent
cf305aeebb
commit
1c94fb5bd1
@ -1,5 +1,7 @@
|
|||||||
from typing import Union, Type, Callable, Optional
|
from typing import Union, Type, Callable, Optional
|
||||||
|
|
||||||
|
import lifetime as lifetime
|
||||||
|
|
||||||
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
||||||
from cpl_core.database.database_settings import DatabaseSettings
|
from cpl_core.database.database_settings import DatabaseSettings
|
||||||
from cpl_core.database.context.database_context_abc import DatabaseContextABC
|
from cpl_core.database.context.database_context_abc import DatabaseContextABC
|
||||||
@ -37,6 +39,14 @@ class ServiceCollection(ServiceCollectionABC):
|
|||||||
|
|
||||||
self._service_descriptors.append(ServiceDescriptor(service, lifetime))
|
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):
|
def add_db_context(self, db_context_type: Type[DatabaseContextABC], db_settings: DatabaseSettings):
|
||||||
self.add_singleton(DatabaseContextABC, db_context_type)
|
self.add_singleton(DatabaseContextABC, db_context_type)
|
||||||
self._database_context = self.build_service_provider().get_service(DatabaseContextABC)
|
self._database_context = self.build_service_provider().get_service(DatabaseContextABC)
|
||||||
@ -46,34 +56,15 @@ class ServiceCollection(ServiceCollectionABC):
|
|||||||
self.add_singleton(LoggerABC, Logger)
|
self.add_singleton(LoggerABC, Logger)
|
||||||
|
|
||||||
def add_singleton(self, service_type: Union[type, object], service: Union[type, object] = None):
|
def add_singleton(self, service_type: Union[type, object], service: Union[type, object] = None):
|
||||||
impl = None
|
self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.singleton, service)
|
||||||
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)
|
|
||||||
|
|
||||||
return self
|
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:
|
self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.scoped, service)
|
||||||
self._add_descriptor(service, ServiceLifetimeEnum.scoped)
|
|
||||||
else:
|
|
||||||
self._add_descriptor(service_type, ServiceLifetimeEnum.scoped)
|
|
||||||
|
|
||||||
return self
|
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:
|
self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.transient, service)
|
||||||
self._add_descriptor(service, ServiceLifetimeEnum.transient)
|
|
||||||
else:
|
|
||||||
self._add_descriptor(service_type, ServiceLifetimeEnum.transient)
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def build_service_provider(self) -> ServiceProviderABC:
|
def build_service_provider(self) -> ServiceProviderABC:
|
||||||
|
@ -7,6 +7,7 @@ from cpl_core.console.console import Console
|
|||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
from cpl_core.logging.logger_abc import LoggerABC
|
from cpl_core.logging.logger_abc import LoggerABC
|
||||||
from cpl_core.mailing import EMailClientABC, EMail
|
from cpl_core.mailing import EMailClientABC, EMail
|
||||||
|
from cpl_core.pipes import IPAddressPipe
|
||||||
from test_service import TestService
|
from test_service import TestService
|
||||||
|
|
||||||
|
|
||||||
@ -43,5 +44,9 @@ class Application(ApplicationABC):
|
|||||||
self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
|
self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
|
||||||
Console.spinner('Test', self._wait, 2, spinner_foreground_color='red')
|
Console.spinner('Test', self._wait, 2, spinner_foreground_color='red')
|
||||||
test: TestService = self._services.get_service(TestService)
|
test: TestService = self._services.get_service(TestService)
|
||||||
|
ip_pipe: IPAddressPipe = self._services.get_service(IPAddressPipe)
|
||||||
test.run()
|
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()
|
# self.test_send_mail()
|
||||||
|
@ -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.application import StartupExtensionABC
|
||||||
from cpl_core.configuration import ConfigurationABC, ArgumentTypeEnum
|
from cpl_core.configuration import ConfigurationABC, ArgumentTypeEnum
|
||||||
from cpl_core.dependency_injection import ServiceCollectionABC
|
from cpl_core.dependency_injection import ServiceCollectionABC
|
||||||
from cpl_core.environment import ApplicationEnvironmentABC
|
from cpl_core.environment import ApplicationEnvironmentABC
|
||||||
from arguments.generate_argument import GenerateArgument
|
|
||||||
from arguments.install_argument import InstallArgument
|
|
||||||
|
|
||||||
|
|
||||||
class ParameterStartup(StartupExtensionABC):
|
class ParameterStartup(StartupExtensionABC):
|
||||||
|
Loading…
Reference in New Issue
Block a user