Bugfixes & improved database context handling
This commit is contained in:
@@ -23,10 +23,7 @@ class ServiceCollection(ServiceCollectionABC):
|
||||
def _add_descriptor(self, service: Union[type, object], lifetime: ServiceLifetimeEnum):
|
||||
found = False
|
||||
for descriptor in self._service_descriptors:
|
||||
if not isinstance(service, type):
|
||||
service = type(service)
|
||||
|
||||
if descriptor.service_type == service:
|
||||
if isinstance(service, descriptor.service_type):
|
||||
found = True
|
||||
|
||||
if found:
|
||||
@@ -39,23 +36,24 @@ class ServiceCollection(ServiceCollectionABC):
|
||||
self._service_descriptors.append(ServiceDescriptor(service, lifetime))
|
||||
|
||||
def add_db_context(self, db_context_type: Type[DatabaseContextABC], db_settings: DatabaseSettings):
|
||||
db_context = db_context_type(db_settings)
|
||||
db_context.connect(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials))
|
||||
self._database_context = db_context_type(db_settings)
|
||||
self._database_context.connect(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials))
|
||||
|
||||
def add_singleton(self, service_type: Union[type, object], service: Union[type, object] = None):
|
||||
impl = None
|
||||
if service is not None:
|
||||
if isinstance(service, type):
|
||||
service = self.build_service_provider().build_service(service)
|
||||
impl = self.build_service_provider().build_service(service)
|
||||
|
||||
self._add_descriptor(service, ServiceLifetimeEnum.singleton)
|
||||
self._add_descriptor(impl, ServiceLifetimeEnum.singleton)
|
||||
else:
|
||||
if isinstance(service_type, type):
|
||||
service_type = self.build_service_provider().build_service(service_type)
|
||||
impl = self.build_service_provider().build_service(service_type)
|
||||
|
||||
self._add_descriptor(service_type, ServiceLifetimeEnum.singleton)
|
||||
self._add_descriptor(impl, ServiceLifetimeEnum.singleton)
|
||||
|
||||
def add_scoped(self, service_type: Type, service: Callable = None):
|
||||
pass
|
||||
raise Exception('Not implemented')
|
||||
|
||||
def add_transient(self, service_type: Union[type], service: Union[type] = None):
|
||||
if service is not None:
|
||||
@@ -64,4 +62,4 @@ class ServiceCollection(ServiceCollectionABC):
|
||||
self._add_descriptor(service_type, ServiceLifetimeEnum.transient)
|
||||
|
||||
def build_service_provider(self) -> ServiceProviderABC:
|
||||
return ServiceProvider(self._service_descriptors, self._configuration)
|
||||
return ServiceProvider(self._service_descriptors, self._configuration, self._database_context)
|
||||
|
@@ -4,6 +4,7 @@ from typing import Optional
|
||||
|
||||
from cpl.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl.database.context.database_context_abc import DatabaseContextABC
|
||||
from cpl.dependency_injection.service_provider_abc import ServiceProviderABC
|
||||
from cpl.dependency_injection.service_descriptor import ServiceDescriptor
|
||||
from cpl.dependency_injection.service_lifetime_enum import ServiceLifetimeEnum
|
||||
@@ -12,11 +13,12 @@ from cpl.environment.application_environment_abc import ApplicationEnvironmentAB
|
||||
|
||||
class ServiceProvider(ServiceProviderABC):
|
||||
|
||||
def __init__(self, service_descriptors: list[ServiceDescriptor], config: ConfigurationABC):
|
||||
def __init__(self, service_descriptors: list[ServiceDescriptor], config: ConfigurationABC, db_context: Optional[DatabaseContextABC]):
|
||||
ServiceProviderABC.__init__(self)
|
||||
|
||||
self._service_descriptors: list[ServiceDescriptor] = service_descriptors
|
||||
self._configuration: ConfigurationABC = config
|
||||
self._database_context = db_context
|
||||
|
||||
def _find_service(self, service_type: type) -> [ServiceDescriptor]:
|
||||
for descriptor in self._service_descriptors:
|
||||
@@ -58,8 +60,8 @@ class ServiceProvider(ServiceProviderABC):
|
||||
elif issubclass(parameter.annotation, ApplicationEnvironmentABC):
|
||||
params.append(self._configuration.environment)
|
||||
|
||||
# elif issubclass(parameter.annotation, DatabaseContextABC):
|
||||
# params.append(self._database_context)
|
||||
elif issubclass(parameter.annotation, DatabaseContextABC):
|
||||
params.append(self._database_context)
|
||||
|
||||
elif issubclass(parameter.annotation, ConfigurationModelABC):
|
||||
params.append(self._configuration.get_configuration(parameter.annotation))
|
||||
|
Reference in New Issue
Block a user