Added docs for cpl.dependency_injection

This commit is contained in:
2021-05-17 17:22:28 +02:00
parent 9026f021ed
commit 4adf6bc834
15 changed files with 182 additions and 86 deletions

View File

@@ -14,6 +14,7 @@ from cpl.utils.credential_manager import CredentialManager
class ServiceCollection(ServiceCollectionABC):
r"""Representation of the collection of services"""
def __init__(self, config: ConfigurationABC):
ServiceCollectionABC.__init__(self)

View File

@@ -8,64 +8,67 @@ from cpl.dependency_injection.service_provider_abc import ServiceProviderABC
class ServiceCollectionABC(ABC):
r"""ABC for the class :class:`cpl.dependency_injection.service_collection.ServiceCollection`"""
@abstractmethod
def __init__(self):
"""
ABC for service providing
"""
pass
@abstractmethod
def add_db_context(self, db_context: Type[DatabaseContextABC], db_settings: DatabaseSettings):
"""
Adds database context
:param db_context:
:param db_settings:
:return:
r"""Adds database context
Parameter
---------
db_context: Type[:class:`cpl.database.context.database_context_abc.DatabaseContextABC`]
db_settings: :class:`cpl.database.database_settings.DatabaseSettings`
"""
pass
@abstractmethod
def add_logging(self):
"""
Adds the CPL internal logger
"""
r"""Adds the CPL internal logger"""
pass
@abstractmethod
def add_transient(self, service_type: Type, service: Callable = None):
"""
Adds a service with transient lifetime
:param service_type:
:param service:
:return:
r"""Adds a service with transient lifetime
Parameter
---------
service_type: :class:`Type`
service: :class:`Callable`
"""
pass
@abstractmethod
def add_scoped(self, service_type: Type, service: Callable = None):
"""
Adds a service with scoped lifetime
:param service_type:
:param service:
:return:
r"""Adds a service with scoped lifetime
Parameter
---------
service_type: :class:`Type`
service: :class:`Callable`
"""
pass
@abstractmethod
def add_singleton(self, service_type: Type, service: Callable = None):
"""
Adds a service with singleton lifetime
:param service_type:
:param service:
:return:
r"""Adds a service with singleton lifetime
Parameter
---------
service_type: :class:`Type`
service: :class:`Callable`
"""
pass
@abstractmethod
def build_service_provider(self) -> ServiceProviderABC:
"""
Creates instance of the service provider
r"""Creates instance of the service provider
Returns
-------
Object of type :class:`cpl.dependency_injection.service_provider_abc.ServiceProviderABC`
"""
pass

View File

@@ -4,6 +4,13 @@ from cpl.dependency_injection.service_lifetime_enum import ServiceLifetimeEnum
class ServiceDescriptor:
r"""Descriptor of a service
Parameter
---------
implementation: Union[:class:`type`, Optional[:class:`object`]]
lifetime: :class:`cpl.dependency_injection.service_lifetime_enum.ServiceLifetimeEnum`
"""
def __init__(self, implementation: Union[type, Optional[object]], lifetime: ServiceLifetimeEnum):

View File

@@ -12,6 +12,14 @@ from cpl.environment.application_environment_abc import ApplicationEnvironmentAB
class ServiceProvider(ServiceProviderABC):
r"""Provider for the services
Parameter
---------
service_descriptors: list[:class:`cpl.dependency_injection.service_descriptor.ServiceDescriptor`]
config: :class:`cpl.configuration.configuration_abc.ConfigurationABC`
db_context: Optional[:class:`cpl.database.context.database_context_abc.DatabaseContextABC`]
"""
def __init__(self, service_descriptors: list[ServiceDescriptor], config: ConfigurationABC, db_context: Optional[DatabaseContextABC]):
ServiceProviderABC.__init__(self)

View File

@@ -4,28 +4,38 @@ from typing import Type, Optional
class ServiceProviderABC(ABC):
r"""ABC for the class :class:`cpl.dependency_injection.service_provider.ServiceProvider`"""
@abstractmethod
def __init__(self):
"""
ABC for service providing
"""
pass
@abstractmethod
def build_service(self, service_type: type) -> object:
"""
Creates instance of given type
:param service_type:
:return:
def build_service(self, service_type: Type) -> object:
r"""Creates instance of given type
Parameter
---------
instance_type: :class:`Type`
The type of the searched instance
Returns
-------
Object of the given type
"""
pass
@abstractmethod
def get_service(self, instance_type: Type) -> Optional[Callable[object]]:
"""
Returns instance of given type
:param instance_type:
:return:
r"""Returns instance of given type
Parameter
---------
instance_type: :class:`Type`
The type of the searched instance
Returns
-------
Object of type Optional[Callable[:class:`object`]]
"""
pass