2020-11-22 14:15:39 +01:00
|
|
|
from collections import Callable
|
2020-11-25 21:41:45 +01:00
|
|
|
from inspect import signature, Parameter
|
2020-11-22 14:15:39 +01:00
|
|
|
from typing import Type
|
|
|
|
|
2020-11-26 10:45:02 +01:00
|
|
|
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
2020-11-26 18:58:08 +01:00
|
|
|
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
|
2020-11-22 20:17:57 +01:00
|
|
|
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
2020-11-22 14:15:39 +01:00
|
|
|
from sh_edraft.service.base.service_base import ServiceBase
|
|
|
|
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
class ServiceProvider(ServiceProviderBase):
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-26 18:58:08 +01:00
|
|
|
def __init__(self, app_runtime: ApplicationRuntimeBase):
|
|
|
|
ServiceProviderBase.__init__(self)
|
|
|
|
self._app_runtime: ApplicationRuntimeBase = app_runtime
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-26 11:55:55 +01:00
|
|
|
self._transient_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
|
|
|
|
self._scoped_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
|
|
|
|
self._singleton_services: dict[Type[ServiceBase], ServiceBase] = {}
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
def create(self): pass
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
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:
|
2020-11-26 18:58:08 +01:00
|
|
|
if issubclass(parameter.annotation, ApplicationRuntimeBase):
|
|
|
|
params.append(self._app_runtime)
|
2020-11-26 11:20:21 +01:00
|
|
|
|
|
|
|
elif issubclass(parameter.annotation, ServiceBase):
|
2020-11-25 21:41:45 +01:00
|
|
|
params.append(self.get_service(parameter.annotation))
|
|
|
|
|
2020-11-26 11:20:21 +01:00
|
|
|
elif issubclass(parameter.annotation, ConfigurationModelBase):
|
2020-11-26 18:58:08 +01:00
|
|
|
params.append(self._app_runtime.configuration.get_config_by_type(parameter.annotation))
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
return service(*params)
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]):
|
|
|
|
self._transient_services[service_type] = service
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
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):
|
2020-11-22 14:15:39 +01:00
|
|
|
for known_service in self._singleton_services:
|
2020-11-25 21:41:45 +01:00
|
|
|
if type(known_service) == type(service_type):
|
|
|
|
raise Exception(f'Service with type {type(service_type)} already exists')
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
self._singleton_services[service_type] = self._create_instance(service)
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
def get_service(self, instance_type: Type[ServiceBase]) -> Callable[ServiceBase]:
|
2020-11-25 21:41:45 +01:00
|
|
|
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])
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
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])
|
2020-11-22 14:15:39 +01:00
|
|
|
|
|
|
|
for service in self._singleton_services:
|
2020-11-25 21:41:45 +01:00
|
|
|
if service == instance_type and isinstance(self._singleton_services[service], instance_type):
|
|
|
|
return self._singleton_services[service]
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
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]
|
2020-11-22 14:15:39 +01:00
|
|
|
return
|
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
for service in self._scoped_services:
|
|
|
|
if isinstance(service, type(instance_type)):
|
|
|
|
del self._scoped_services[service]
|
2020-11-22 14:15:39 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
for service in self._singleton_services:
|
2020-11-22 20:17:57 +01:00
|
|
|
if isinstance(service, instance_type):
|
2020-11-25 21:41:45 +01:00
|
|
|
del self._singleton_services[service]
|
2020-11-22 14:15:39 +01:00
|
|
|
return
|