Improved testing and service providing
This commit is contained in:
@@ -6,8 +6,5 @@ class ServiceBase(ABC):
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def init(self, args: tuple): pass
|
||||
|
||||
@abstractmethod
|
||||
def create(self): pass
|
||||
|
||||
@@ -3,7 +3,6 @@ from collections import Callable
|
||||
from typing import Type
|
||||
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.model.provide_state import ProvideState
|
||||
|
||||
|
||||
class ServiceProviderBase(ServiceBase):
|
||||
@@ -11,18 +10,23 @@ class ServiceProviderBase(ServiceBase):
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
self._transient_services: list[ProvideState] = []
|
||||
self._scoped_services: list[ProvideState] = []
|
||||
self._singleton_services: list[ServiceBase] = []
|
||||
|
||||
self._transient_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
|
||||
self._scoped_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
|
||||
self._singleton_services: dict[Type[ServiceBase], ServiceBase] = {}
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def config(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def add_transient(self, service: Type[ServiceBase], *args): pass
|
||||
def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]): pass
|
||||
|
||||
@abstractmethod
|
||||
def add_scoped(self, service: Type[ServiceBase], *args): pass
|
||||
def add_scoped(self, service_type: Type[ServiceBase], service: Type[ServiceBase]): pass
|
||||
|
||||
@abstractmethod
|
||||
def add_singleton(self, service: Type[ServiceBase], *args): pass
|
||||
def add_singleton(self, service_type: Type[ServiceBase], service: ServiceBase): pass
|
||||
|
||||
@abstractmethod
|
||||
def get_service(self, instance_type: Type[ServiceBase]) -> Callable[ServiceBase]: pass
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from collections import Callable
|
||||
from inspect import signature, Parameter
|
||||
from typing import Type
|
||||
|
||||
from termcolor import colored
|
||||
|
||||
from sh_edraft.configuration.configuration import Configuration
|
||||
from sh_edraft.configuration.model.application_host_base import ApplicationHostBase
|
||||
from sh_edraft.configuration.model.configuration_model_base import ConfigurationModelBase
|
||||
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.model.provide_state import ProvideState
|
||||
@@ -12,58 +14,71 @@ class ServiceProvider(ServiceProviderBase):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._config = Configuration()
|
||||
|
||||
def init(self, args: tuple): pass
|
||||
@property
|
||||
def config(self):
|
||||
return self._config
|
||||
|
||||
def create(self): pass
|
||||
|
||||
@staticmethod
|
||||
def _create_instance(service: type[ServiceBase], args: tuple) -> ServiceBase:
|
||||
instance = service()
|
||||
try:
|
||||
instance.init(args)
|
||||
return instance
|
||||
except Exception as e:
|
||||
print(colored(f'Argument error\n{e}', 'red'))
|
||||
def _create_instance(self, service: Callable[ServiceBase]) -> ServiceBase:
|
||||
sig = signature(service.__init__)
|
||||
params = []
|
||||
for param in sig.parameters.items():
|
||||
parameter = param[1]
|
||||
if parameter.name != 'self' and parameter.annotation != Parameter.empty:
|
||||
if issubclass(parameter.annotation, ServiceBase):
|
||||
params.append(self.get_service(parameter.annotation))
|
||||
|
||||
def add_transient(self, service: Type[ServiceBase], *args):
|
||||
self._transient_services.append(ProvideState(service, args))
|
||||
elif issubclass(parameter.annotation, ConfigurationModelBase) or issubclass(parameter.annotation, ApplicationHostBase):
|
||||
params.append(self._config.get_config_by_type(parameter.annotation))
|
||||
|
||||
def add_scoped(self, service: Type[ServiceBase], *args):
|
||||
self._scoped_services.append(ProvideState(service, args))
|
||||
return service(*params)
|
||||
# try:
|
||||
# instance.init(args)
|
||||
# return instance
|
||||
# except Exception as e:
|
||||
# print(colored(f'Argument error\n{e}', 'red'))
|
||||
|
||||
def add_singleton(self, service: Type[ServiceBase], *args):
|
||||
def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]):
|
||||
self._transient_services[service_type] = service
|
||||
|
||||
def add_scoped(self, service_type: Type[ServiceBase], service: Type[ServiceBase]):
|
||||
self._scoped_services[service_type] = service
|
||||
|
||||
def add_singleton(self, service_type: Type[ServiceBase], service: ServiceBase):
|
||||
for known_service in self._singleton_services:
|
||||
if type(known_service) == type(service):
|
||||
raise Exception(f'Service from type {type(service)} already exists')
|
||||
if type(known_service) == type(service_type):
|
||||
raise Exception(f'Service with type {type(service_type)} already exists')
|
||||
|
||||
self._singleton_services.append(self._create_instance(service, args))
|
||||
self._singleton_services[service_type] = self._create_instance(service)
|
||||
|
||||
def get_service(self, instance_type: Type[ServiceBase]) -> Callable[ServiceBase]:
|
||||
for state in self._transient_services:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
return self._create_instance(state.service, state.args)
|
||||
for service in self._transient_services:
|
||||
if service == instance_type and isinstance(self._transient_services[service], type(instance_type)):
|
||||
return self._create_instance(self._transient_services[service])
|
||||
|
||||
for state in self._scoped_services:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
return self._create_instance(state.service, state.args)
|
||||
for service in self._scoped_services:
|
||||
if service == instance_type and isinstance(self._scoped_services[service], type(instance_type)):
|
||||
return self._create_instance(self._scoped_services[service])
|
||||
|
||||
for service in self._singleton_services:
|
||||
if isinstance(service, instance_type):
|
||||
return service
|
||||
if service == instance_type and isinstance(self._singleton_services[service], instance_type):
|
||||
return self._singleton_services[service]
|
||||
|
||||
def remove_service(self, instance_type: type):
|
||||
for state in self._transient_services:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
self._transient_services.remove(state)
|
||||
def remove_service(self, instance_type: Type[ServiceBase]):
|
||||
for service in self._transient_services:
|
||||
if isinstance(service, type(instance_type)):
|
||||
del self._transient_services[service]
|
||||
return
|
||||
|
||||
for state in self._scoped_services:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
self._scoped_services.remove(state)
|
||||
for service in self._scoped_services:
|
||||
if isinstance(service, type(instance_type)):
|
||||
del self._scoped_services[service]
|
||||
return
|
||||
|
||||
for service in self._singleton_services:
|
||||
if isinstance(service, instance_type):
|
||||
self._singleton_services.remove(service)
|
||||
del self._singleton_services[service]
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user