Added logic to add internal pipes in ServiceCollection

This commit is contained in:
Sven Heidemann 2022-05-25 18:42:47 +02:00
parent 15f200df3a
commit e23f4187fe
3 changed files with 12 additions and 1 deletions

View File

@ -1,6 +1,7 @@
from typing import Union, Type, Callable, Optional from typing import Union, Type, Callable, Optional
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console import Console
from cpl_core.database.context.database_context_abc import DatabaseContextABC from cpl_core.database.context.database_context_abc import DatabaseContextABC
from cpl_core.database.database_settings import DatabaseSettings from cpl_core.database.database_settings import DatabaseSettings
from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC
@ -10,6 +11,7 @@ from cpl_core.dependency_injection.service_provider import ServiceProvider
from cpl_core.dependency_injection.service_provider_abc import ServiceProviderABC from cpl_core.dependency_injection.service_provider_abc import ServiceProviderABC
from cpl_core.logging.logger_abc import LoggerABC from cpl_core.logging.logger_abc import LoggerABC
from cpl_core.logging.logger_service import Logger from cpl_core.logging.logger_service import Logger
from cpl_core.pipes.pipe_abc import PipeABC
class ServiceCollection(ServiceCollectionABC): class ServiceCollection(ServiceCollectionABC):
@ -53,6 +55,10 @@ class ServiceCollection(ServiceCollectionABC):
def add_logging(self): def add_logging(self):
self.add_singleton(LoggerABC, Logger) self.add_singleton(LoggerABC, Logger)
def add_pipes(self):
for pipe in PipeABC.__subclasses__():
self.add_transient(PipeABC, pipe)
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):
self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.singleton, service) self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.singleton, service)
return self return self

View File

@ -30,6 +30,11 @@ class ServiceCollectionABC(ABC):
r"""Adds the CPL internal logger""" r"""Adds the CPL internal logger"""
pass pass
@abstractmethod
def add_pipes(self):
r"""Adds the CPL internal pipes as transient"""
pass
@abstractmethod @abstractmethod
def add_transient(self, service_type: Type, service: Callable = None) -> 'ServiceCollectionABC': 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

View File

@ -24,8 +24,8 @@ class Startup(StartupABC):
return configuration return configuration
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
services.add_pipes()
services.add_transient(GitService) services.add_transient(GitService)
services.add_transient(VersionSetterService) services.add_transient(VersionSetterService)
services.add_transient(VersionPipe)
return services.build_service_provider() return services.build_service_provider()