Improved scope handling

This commit is contained in:
Sven Heidemann 2022-07-31 13:32:02 +02:00
parent 55609fc691
commit 88e83db330
3 changed files with 13 additions and 1 deletions

View File

@ -9,6 +9,13 @@ class Scope(ScopeABC):
def __init__(self, service_provider: ServiceProviderABC): def __init__(self, service_provider: ServiceProviderABC):
self._service_provider = service_provider self._service_provider = service_provider
self._service_provider.set_scope(self) self._service_provider.set_scope(self)
ScopeABC.__init__(self)
def __enter__(self):
return self
def __exit__(self, *args):
self.dispose()
@property @property
def service_provider(self) -> ServiceProviderABC: def service_provider(self) -> ServiceProviderABC:

View File

@ -18,7 +18,7 @@ class ScopeABC(ABC):
pass pass
@abstractmethod @abstractmethod
def dispose(): def dispose(self):
r"""Sets service_provider to None r"""Sets service_provider to None
""" """
pass pass

View File

@ -53,4 +53,9 @@ class Application(ApplicationABC):
ip_pipe2: IPAddressPipe = self._services.get_service(IPAddressPipe) ip_pipe2: IPAddressPipe = self._services.get_service(IPAddressPipe)
Console.write_line(f'DI working: {test == test2 and ip_pipe != ip_pipe2}') Console.write_line(f'DI working: {test == test2 and ip_pipe != ip_pipe2}')
Console.write_line(self._services.get_service(LoggerABC)) Console.write_line(self._services.get_service(LoggerABC))
scope = self._services.create_scope()
Console.write_line('scope', scope)
with self._services.create_scope() as s:
Console.write_line('with scope', s)
# self.test_send_mail() # self.test_send_mail()