Moved tests to src & added add scoped logic

This commit is contained in:
Sven Heidemann 2021-11-14 10:04:32 +01:00
parent c3b48bfa41
commit 7749d5a789
83 changed files with 9 additions and 5 deletions

View File

@ -59,9 +59,13 @@ class ServiceCollection(ServiceCollectionABC):
self._add_descriptor(impl, ServiceLifetimeEnum.singleton) self._add_descriptor(impl, ServiceLifetimeEnum.singleton)
def add_scoped(self, service_type: Type, service: Callable = None): def add_scoped(self, service_type: Type, service: Callable = None):
raise Exception('Not implemented') if service is not None:
self._add_descriptor(service, ServiceLifetimeEnum.scoped)
else:
self._add_descriptor(service_type, ServiceLifetimeEnum.scoped)
def add_transient(self, service_type: Union[type], service: Union[type] = None): def add_transient(self, service_type: type, service: type = None):
if service is not None: if service is not None:
self._add_descriptor(service, ServiceLifetimeEnum.transient) self._add_descriptor(service, ServiceLifetimeEnum.transient)
else: else:

View File

@ -31,7 +31,7 @@ class ServiceProvider(ServiceProviderABC):
self._configuration: ConfigurationABC = config self._configuration: ConfigurationABC = config
self._database_context = db_context self._database_context = db_context
def _find_service(self, service_type: type) -> [ServiceDescriptor]: def _find_service(self, service_type: type) -> ServiceDescriptor:
for descriptor in self._service_descriptors: for descriptor in self._service_descriptors:
if descriptor.service_type == service_type or issubclass(descriptor.service_type, service_type): if descriptor.service_type == service_type or issubclass(descriptor.service_type, service_type):
return descriptor return descriptor
@ -85,7 +85,7 @@ class ServiceProvider(ServiceProviderABC):
return service_type(*params) return service_type(*params)
def get_service(self, service_type: type) -> Optional[Callable[object]]: def get_service(self, service_type: type) -> Optional[Callable]:
result = self._find_service(service_type) result = self._find_service(service_type)
if result is None: if result is None:

View File

@ -26,7 +26,7 @@ class ServiceProviderABC(ABC):
pass pass
@abstractmethod @abstractmethod
def get_service(self, instance_type: Type) -> Optional[Callable[object]]: def get_service(self, instance_type: Type) -> Optional[Callable]:
r"""Returns instance of given type r"""Returns instance of given type
Parameter Parameter