New implementation of scopes #186
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
from cpl.application.abc import ApplicationABC
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.dependency import ServiceProvider
|
||||
from cpl.dependency.scope import Scope
|
||||
from di.static_test import StaticTest
|
||||
from di.test_abc import TestABC
|
||||
from di.test_service import TestService
|
||||
@@ -17,26 +16,30 @@ class Application(ApplicationABC):
|
||||
ts: TestService = self._services.get_service(TestService)
|
||||
ts.run()
|
||||
|
||||
def configure(self): ...
|
||||
|
||||
def main(self):
|
||||
with self._services.create_scope() as scope:
|
||||
Console.write_line("Scope1")
|
||||
ts: TestService = scope.service_provider.get_service(TestService)
|
||||
ts: TestService = scope.get_service(TestService)
|
||||
ts.run()
|
||||
dit: DITesterService = scope.service_provider.get_service(DITesterService)
|
||||
dit: DITesterService = scope.get_service(DITesterService)
|
||||
dit.run()
|
||||
|
||||
if ts.name != dit.name:
|
||||
raise Exception("DI is broken!")
|
||||
|
||||
with self._services.create_scope() as scope:
|
||||
Console.write_line("Scope2")
|
||||
ts: TestService = scope.service_provider.get_service(TestService)
|
||||
ts: TestService = scope.get_service(TestService)
|
||||
ts.run()
|
||||
dit: DITesterService = scope.service_provider.get_service(DITesterService)
|
||||
dit: DITesterService = scope.get_service(DITesterService)
|
||||
dit.run()
|
||||
|
||||
if ts.name != dit.name:
|
||||
raise Exception("DI is broken!")
|
||||
|
||||
Console.write_line("Global")
|
||||
self._part_of_scoped()
|
||||
StaticTest.test()
|
||||
|
||||
self._services.get_service(Tester)
|
||||
Console.write_line(self._services.get_services(list[TestABC]))
|
||||
Console.write_line(self._services.get_services(TestABC))
|
||||
|
||||
@@ -6,6 +6,10 @@ class DITesterService:
|
||||
def __init__(self, ts: TestService):
|
||||
self._ts = ts
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._ts.name
|
||||
|
||||
def run(self):
|
||||
Console.write_line("DIT: ")
|
||||
self._ts.run()
|
||||
|
||||
@@ -12,9 +12,11 @@ class Startup(StartupABC):
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
def configure_configuration(self): ...
|
||||
@staticmethod
|
||||
def configure_configuration(): ...
|
||||
|
||||
def configure_services(self, services: ServiceCollection) -> ServiceProvider:
|
||||
@staticmethod
|
||||
def configure_services(services: ServiceCollection) -> ServiceProvider:
|
||||
services.add_scoped(TestService)
|
||||
services.add_scoped(DITesterService)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from di.test_abc import TestABC
|
||||
|
||||
class Test1Service(TestABC):
|
||||
def __init__(self):
|
||||
TestABC.__init__(self, String.random_string(string.ascii_lowercase, 8))
|
||||
TestABC.__init__(self, String.random(8))
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f"Im {self._name}")
|
||||
|
||||
@@ -6,7 +6,7 @@ from di.test_abc import TestABC
|
||||
|
||||
class Test2Service(TestABC):
|
||||
def __init__(self):
|
||||
TestABC.__init__(self, String.random_string(string.ascii_lowercase, 8))
|
||||
TestABC.__init__(self, String.random(8))
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f"Im {self._name}")
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import string
|
||||
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.core.utils.string import String
|
||||
|
||||
@@ -8,5 +6,9 @@ class TestService:
|
||||
def __init__(self):
|
||||
self._name = String.random(8)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f"Im {self._name}")
|
||||
|
||||
Reference in New Issue
Block a user