Added multiple instance handling #152
This commit is contained in:
@@ -4,8 +4,10 @@ from cpl_core.console.console import Console
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from cpl_core.dependency_injection.scope import Scope
|
||||
from di.static_test import StaticTest
|
||||
from di.test_service_service import TestService
|
||||
from di.test_abc import TestABC
|
||||
from di.test_service import TestService
|
||||
from di.di_tester_service import DITesterService
|
||||
from di.tester import Tester
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
@@ -28,10 +30,6 @@ class Application(ApplicationABC):
|
||||
dit: DITesterService = scope.service_provider.get_service(DITesterService)
|
||||
dit.run()
|
||||
|
||||
# Console.write_line('Disposed:')
|
||||
# ts1: TestService = scope1.service_provider.get_service(TestService)
|
||||
# ts1.run()
|
||||
|
||||
with self._services.create_scope() as scope:
|
||||
Console.write_line('Scope2')
|
||||
ts: TestService = scope.service_provider.get_service(TestService)
|
||||
@@ -42,5 +40,6 @@ class Application(ApplicationABC):
|
||||
Console.write_line('Global')
|
||||
self._part_of_scoped()
|
||||
StaticTest.test()
|
||||
with self._services.create_scope() as scope:
|
||||
StaticTest.test()
|
||||
|
||||
self._services.get_service(Tester)
|
||||
Console.write_line(self._services.get_services(list[TestABC]))
|
||||
|
@@ -1,5 +1,5 @@
|
||||
from cpl_core.console.console import Console
|
||||
from di.test_service_service import TestService
|
||||
from di.test_service import TestService
|
||||
|
||||
|
||||
class DITesterService:
|
||||
|
@@ -2,8 +2,12 @@ from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
||||
from cpl_core.environment import ApplicationEnvironment
|
||||
from di.test_service_service import TestService
|
||||
from di.test1_service import Test1Service
|
||||
from di.test2_service import Test2Service
|
||||
from di.test_abc import TestABC
|
||||
from di.test_service import TestService
|
||||
from di.di_tester_service import DITesterService
|
||||
from di.tester import Tester
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
@@ -17,5 +21,9 @@ class Startup(StartupABC):
|
||||
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
||||
services.add_scoped(TestService)
|
||||
services.add_scoped(DITesterService)
|
||||
|
||||
services.add_singleton(TestABC, Test1Service)
|
||||
services.add_singleton(TestABC, Test2Service)
|
||||
services.add_singleton(Tester)
|
||||
|
||||
return services.build_service_provider()
|
||||
|
@@ -1,6 +1,6 @@
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProvider, ServiceProviderABC
|
||||
from di.test_service_service import TestService
|
||||
from di.test_service import TestService
|
||||
|
||||
|
||||
class StaticTest:
|
||||
|
13
tests/custom/di/src/di/test1_service.py
Normal file
13
tests/custom/di/src/di/test1_service.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import string
|
||||
from cpl_core.console.console import Console
|
||||
from cpl_core.utils.string import String
|
||||
from di.test_abc import TestABC
|
||||
|
||||
|
||||
class Test1Service(TestABC):
|
||||
|
||||
def __init__(self):
|
||||
TestABC.__init__(self, String.random_string(string.ascii_lowercase, 8))
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f'Im {self._name}')
|
13
tests/custom/di/src/di/test2_service.py
Normal file
13
tests/custom/di/src/di/test2_service.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import string
|
||||
from cpl_core.console.console import Console
|
||||
from cpl_core.utils.string import String
|
||||
from di.test_abc import TestABC
|
||||
|
||||
|
||||
class Test2Service(TestABC):
|
||||
|
||||
def __init__(self):
|
||||
TestABC.__init__(self, String.random_string(string.ascii_lowercase, 8))
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f'Im {self._name}')
|
10
tests/custom/di/src/di/test_abc.py
Normal file
10
tests/custom/di/src/di/test_abc.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from abc import ABC
|
||||
|
||||
|
||||
class TestABC(ABC):
|
||||
|
||||
def __init__(self, name: str):
|
||||
self._name = name
|
||||
|
||||
def __repr__(self):
|
||||
return f'<{type(self).__name__} {self._name}>'
|
@@ -1,4 +1,5 @@
|
||||
import string
|
||||
|
||||
from cpl_core.console.console import Console
|
||||
from cpl_core.utils.string import String
|
||||
|
||||
@@ -7,7 +8,6 @@ class TestService:
|
||||
|
||||
def __init__(self):
|
||||
self._name = String.random_string(string.ascii_lowercase, 8)
|
||||
|
||||
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f'Im {self._name}')
|
||||
Console.write_line(f'Im {self._name}')
|
9
tests/custom/di/src/di/tester.py
Normal file
9
tests/custom/di/src/di/tester.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from cpl_core.console.console import Console
|
||||
from di.test_abc import TestABC
|
||||
|
||||
|
||||
class Tester:
|
||||
|
||||
def __init__(self, t1: TestABC, t2: TestABC, t3: list[TestABC]):
|
||||
Console.write_line('Tester:')
|
||||
Console.write_line(t1, t2, t3)
|
Reference in New Issue
Block a user