Improved dependency injection

This commit is contained in:
Sven Heidemann 2021-03-04 19:06:16 +01:00
parent 6452226b50
commit 69211cc9e8
7 changed files with 74 additions and 7 deletions

View File

@ -134,7 +134,7 @@ class Configuration(ConfigurationABC):
exit() exit()
def add_json_file(self, name: str, optional: bool = None, output: bool = False): def add_json_file(self, name: str, optional: bool = None, output: bool = True):
if self._hosting_environment.content_root_path.endswith('/') and not name.startswith('/'): if self._hosting_environment.content_root_path.endswith('/') and not name.startswith('/'):
file_path = f'{self._hosting_environment.content_root_path}{name}' file_path = f'{self._hosting_environment.content_root_path}{name}'
else: else:
@ -177,8 +177,7 @@ class Configuration(ConfigurationABC):
def add_configuration(self, key_type: type, value: ConfigurationModelABC): def add_configuration(self, key_type: type, value: ConfigurationModelABC):
self._config[key_type] = value self._config[key_type] = value
def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[ def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[str, Callable[ConfigurationModelABC]]:
str, Callable[ConfigurationModelABC]]:
if search_type not in self._config: if search_type not in self._config:
raise Exception(f'Config model by type {search_type} not found') raise Exception(f'Config model by type {search_type} not found')

View File

@ -37,7 +37,7 @@ class ConfigurationABC(ABC):
def add_console_arguments(self): pass def add_console_arguments(self): pass
@abstractmethod @abstractmethod
def add_json_file(self, name: str, optional: bool = None, output: bool = False): pass def add_json_file(self, name: str, optional: bool = None, output: bool = True): pass
@abstractmethod @abstractmethod
def add_configuration(self, key_type: type, value: object): pass def add_configuration(self, key_type: type, value: object): pass

View File

@ -1,11 +1,9 @@
from abc import ABC
from collections import Callable from collections import Callable
from inspect import signature, Parameter from inspect import signature, Parameter
from typing import Type, Optional, Union from typing import Type, Optional, Union
from cpl.application.application_runtime_abc import ApplicationRuntimeABC from cpl.application.application_runtime_abc import ApplicationRuntimeABC
from cpl.configuration.configuration_model_abc import ConfigurationModelABC from cpl.configuration.configuration_model_abc import ConfigurationModelABC
from cpl.console.console import Console
from cpl.database.context.database_context_abc import DatabaseContextABC from cpl.database.context.database_context_abc import DatabaseContextABC
from cpl.dependency_injection.service_abc import ServiceABC from cpl.dependency_injection.service_abc import ServiceABC
from cpl.dependency_injection.service_provider_base import ServiceProviderABC from cpl.dependency_injection.service_provider_base import ServiceProviderABC
@ -41,6 +39,9 @@ class ServiceProvider(ServiceProviderABC):
elif issubclass(parameter.annotation, ConfigurationModelABC): elif issubclass(parameter.annotation, ConfigurationModelABC):
params.append(self._app_runtime.configuration.get_configuration(parameter.annotation)) params.append(self._app_runtime.configuration.get_configuration(parameter.annotation))
elif issubclass(parameter.annotation, ServiceProviderABC):
params.append(self)
else: else:
params.append(self.get_service(parameter.annotation)) params.append(self.get_service(parameter.annotation))
@ -75,6 +76,9 @@ class ServiceProvider(ServiceProviderABC):
self._singleton_services[service_type] = self._create_instance(service) self._singleton_services[service_type] = self._create_instance(service)
def get_service(self, instance_type: Type) -> Callable[ServiceABC]: def get_service(self, instance_type: Type) -> Callable[ServiceABC]:
if issubclass(instance_type, ServiceProviderABC):
return self
for service in self._transient_services: for service in self._transient_services:
if service == instance_type and isinstance(self._transient_services[service], type(instance_type)): if service == instance_type and isinstance(self._transient_services[service], type(instance_type)):
return self._create_instance(self._transient_services[service]) return self._create_instance(self._transient_services[service])

View File

@ -0,0 +1,46 @@
from typing import Optional
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
from cpl.version.version_enum import VersionEnum
class Version(ConfigurationModelABC):
def __init__(
self,
major: int = None,
minor: int = None,
micro: float = None
):
ConfigurationModelABC.__init__(self)
self._major: Optional[int] = major
self._minor: Optional[int] = minor
self._micro: Optional[int] = micro
@property
def major(self) -> int:
return self._major
@property
def minor(self) -> int:
return self._minor
@property
def micro(self) -> float:
return self._micro
def to_str(self) -> str:
return f'{self._major}.{self._minor}.{self._micro}'
def from_dict(self, settings: dict):
self._major = int(settings[VersionEnum.Major.value])
self._minor = int(settings[VersionEnum.Minor.value])
self._micro = int(settings[VersionEnum.Micro.value])
def to_dict(self) -> dict:
return {
VersionEnum.Major.value: self._major,
VersionEnum.Minor.value: self._minor,
VersionEnum.Micro.value: self._micro
}

View File

@ -0,0 +1,8 @@
from enum import Enum
class VersionEnum(Enum):
Major = 'Major'
Minor = 'Minor'
Micro = 'Micro'

View File

@ -1 +1,11 @@
{} {
"ProjectSettings": {
"Version": {
"major": "2021",
"minor": "04",
"micro": "01"
},
"ExcludedFiles": [],
"DistPath": "../../dist"
}
}