Added comments to dependency_injection

This commit is contained in:
Sven Heidemann 2021-03-14 16:25:11 +01:00
parent d1f1627214
commit 268d56c348
3 changed files with 70 additions and 9 deletions

View File

@ -4,4 +4,8 @@ from abc import ABC, abstractmethod
class ServiceABC(ABC): class ServiceABC(ABC):
@abstractmethod @abstractmethod
def __init__(self): pass def __init__(self):
"""
ABC to represent a service
"""
pass

View File

@ -14,6 +14,10 @@ from cpl.environment.environment_abc import ApplicationEnvironmentABC
class ServiceProvider(ServiceProviderABC): class ServiceProvider(ServiceProviderABC):
def __init__(self, app_runtime: ApplicationRuntimeABC): def __init__(self, app_runtime: ApplicationRuntimeABC):
"""
Service for service providing
:param app_runtime:
"""
ServiceProviderABC.__init__(self) ServiceProviderABC.__init__(self)
self._app_runtime: ApplicationRuntimeABC = app_runtime self._app_runtime: ApplicationRuntimeABC = app_runtime
self._database_context: Optional[DatabaseContextABC] = None self._database_context: Optional[DatabaseContextABC] = None
@ -23,6 +27,11 @@ class ServiceProvider(ServiceProviderABC):
self._singleton_services: dict[Type[ServiceABC], Callable[ServiceABC], ServiceABC] = {} self._singleton_services: dict[Type[ServiceABC], Callable[ServiceABC], ServiceABC] = {}
def _create_instance(self, service: Union[Callable[ServiceABC], ServiceABC]) -> Callable[ServiceABC]: def _create_instance(self, service: Union[Callable[ServiceABC], ServiceABC]) -> Callable[ServiceABC]:
"""
Creates an instance of given type
:param service:
:return:
"""
sig = signature(service.__init__) sig = signature(service.__init__)
params = [] params = []
for param in sig.parameters.items(): for param in sig.parameters.items():

View File

@ -9,25 +9,73 @@ from cpl.dependency_injection.service_abc import ServiceABC
class ServiceProviderABC(ABC): class ServiceProviderABC(ABC):
@abstractmethod @abstractmethod
def __init__(self): pass def __init__(self):
"""
ABC for service providing
"""
pass
@abstractmethod @abstractmethod
def add_db_context(self, db_context: Type[DatabaseContextABC]): pass def add_db_context(self, db_context: Type[DatabaseContextABC]):
"""
Adds database context
:param db_context:
:return:
"""
pass
@abstractmethod @abstractmethod
def get_db_context(self) -> Callable[DatabaseContextABC]: pass def get_db_context(self) -> Callable[DatabaseContextABC]:
""""
Returns database context
:return Callable[DatabaseContextABC]:
"""
pass
@abstractmethod @abstractmethod
def add_transient(self, service_type: Type, service: Callable = None): pass def add_transient(self, service_type: Type, service: Callable = None):
"""
Adds a service with transient lifetime
:param service_type:
:param service:
:return:
"""
pass
@abstractmethod @abstractmethod
def add_scoped(self, service_type: Type, service: Callable = None): pass def add_scoped(self, service_type: Type, service: Callable = None):
"""
Adds a service with scoped lifetime
:param service_type:
:param service:
:return:
"""
pass
@abstractmethod @abstractmethod
def add_singleton(self, service_type: Type, service: Callable = None): pass def add_singleton(self, service_type: Type, service: Callable = None):
"""
Adds a service with singleton lifetime
:param service_type:
:param service:
:return:
"""
pass
@abstractmethod @abstractmethod
def get_service(self, instance_type: Type) -> Callable[ServiceABC]: pass def get_service(self, instance_type: Type) -> Callable[ServiceABC]:
"""
Returns instance of given type
:param instance_type:
:return:
"""
pass
@abstractmethod @abstractmethod
def remove_service(self, instance_type: type): pass def remove_service(self, instance_type: type):
"""
Removes service
:param instance_type:
:return:
"""
pass