Improved service providing and app hosting

This commit is contained in:
2020-11-26 18:58:08 +01:00
parent cd7c12bba4
commit c6d1dce577
11 changed files with 118 additions and 79 deletions

View File

@@ -11,10 +11,6 @@ class ServiceProviderBase(ServiceBase):
def __init__(self):
ServiceBase.__init__(self)
@property
@abstractmethod
def config(self): pass
@abstractmethod
def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]): pass

View File

@@ -2,28 +2,22 @@ from collections import Callable
from inspect import signature, Parameter
from typing import Type
from sh_edraft.configuration.configuration import Configuration
from sh_edraft.hosting.base.application_host_base import ApplicationHostBase
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
from sh_edraft.service.base.service_base import ServiceBase
class ServiceProvider(ServiceProviderBase):
def __init__(self, app_host: ApplicationHostBase):
super().__init__()
self._app_host: ApplicationHostBase = app_host
self._config = Configuration()
def __init__(self, app_runtime: ApplicationRuntimeBase):
ServiceProviderBase.__init__(self)
self._app_runtime: ApplicationRuntimeBase = app_runtime
self._transient_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
self._scoped_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
self._singleton_services: dict[Type[ServiceBase], ServiceBase] = {}
@property
def config(self):
return self._config
def create(self): pass
def _create_instance(self, service: Callable[ServiceBase]) -> ServiceBase:
@@ -32,21 +26,16 @@ class ServiceProvider(ServiceProviderBase):
for param in sig.parameters.items():
parameter = param[1]
if parameter.name != 'self' and parameter.annotation != Parameter.empty:
if issubclass(parameter.annotation, ApplicationHostBase):
params.append(self._app_host)
if issubclass(parameter.annotation, ApplicationRuntimeBase):
params.append(self._app_runtime)
elif issubclass(parameter.annotation, ServiceBase):
params.append(self.get_service(parameter.annotation))
elif issubclass(parameter.annotation, ConfigurationModelBase):
params.append(self._config.get_config_by_type(parameter.annotation))
params.append(self._app_runtime.configuration.get_config_by_type(parameter.annotation))
return service(*params)
# try:
# instance.init(args)
# return instance
# except Exception as e:
# print(colored(f'Argument error\n{e}', 'red'))
def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]):
self._transient_services[service_type] = service