Improved project file structure
This commit is contained in:
1
tests/custom/di/src/di/__init__.py
Normal file
1
tests/custom/di/src/di/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports:
|
45
tests/custom/di/src/di/application.py
Normal file
45
tests/custom/di/src/di/application.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from cpl_core.application import ApplicationABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.console.console import Console
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from cpl_core.dependency_injection.scope import Scope
|
||||
from test_service_service import TestService
|
||||
from di_tester_service import DITesterService
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
ApplicationABC.__init__(self, config, services)
|
||||
|
||||
def _part_of_scoped(self):
|
||||
ts: TestService = self._services.get_service(TestService)
|
||||
ts.run()
|
||||
|
||||
def configure(self):
|
||||
pass
|
||||
|
||||
def main(self):
|
||||
Console.write_line('Scope1')
|
||||
scope1: Scope = self._services.create_scope()
|
||||
ts: TestService = scope1.service_provider.get_service(TestService)
|
||||
ts.run()
|
||||
dit: DITesterService = scope1.service_provider.get_service(DITesterService)
|
||||
dit.run()
|
||||
t = scope1
|
||||
b = t.service_provider
|
||||
scope1.dispose()
|
||||
|
||||
#Console.write_line('Disposed:')
|
||||
#ts1: TestService = scope1.service_provider.get_service(TestService)
|
||||
#ts1.run()
|
||||
|
||||
Console.write_line('Scope2')
|
||||
scope2: Scope = self._services.create_scope()
|
||||
ts: TestService = scope2.service_provider.get_service(TestService)
|
||||
ts.run()
|
||||
dit: DITesterService = scope2.service_provider.get_service(DITesterService)
|
||||
dit.run()
|
||||
|
||||
Console.write_line('Global')
|
||||
self._part_of_scoped()
|
43
tests/custom/di/src/di/di.json
Normal file
43
tests/custom/di/src/di/di.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "di",
|
||||
"Version": {
|
||||
"Major": "0",
|
||||
"Minor": "0",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"URL": "",
|
||||
"CopyrightDate": "",
|
||||
"CopyrightName": "",
|
||||
"LicenseName": "",
|
||||
"LicenseDescription": "",
|
||||
"Dependencies": [
|
||||
"sh_cpl>=2021.10.0.post1"
|
||||
],
|
||||
"PythonVersion": ">=3.9.2",
|
||||
"PythonPath": {
|
||||
"linux": ""
|
||||
},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "console",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "di.main",
|
||||
"EntryPoint": "di",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
12
tests/custom/di/src/di/di_tester_service.py
Normal file
12
tests/custom/di/src/di/di_tester_service.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from cpl_core.console.console import Console
|
||||
from test_service_service import TestService
|
||||
|
||||
|
||||
class DITesterService:
|
||||
|
||||
def __init__(self, ts: TestService):
|
||||
self._ts = ts
|
||||
|
||||
def run(self):
|
||||
Console.write_line('DIT: ')
|
||||
self._ts.run()
|
14
tests/custom/di/src/di/main.py
Normal file
14
tests/custom/di/src/di/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from di.application import Application
|
||||
from di.startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
app_builder.build().run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
21
tests/custom/di/src/di/startup.py
Normal file
21
tests/custom/di/src/di/startup.py
Normal file
@@ -0,0 +1,21 @@
|
||||
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 test_service_service import TestService
|
||||
from di_tester_service import DITesterService
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
|
||||
return configuration
|
||||
|
||||
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
||||
services.add_scoped(TestService)
|
||||
services.add_scoped(DITesterService)
|
||||
|
||||
return services.build_service_provider()
|
13
tests/custom/di/src/di/test_service_service.py
Normal file
13
tests/custom/di/src/di/test_service_service.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import string
|
||||
from cpl_core.console.console import Console
|
||||
from cpl_core.utils.string import String
|
||||
|
||||
|
||||
class TestService:
|
||||
|
||||
def __init__(self):
|
||||
self._name = String.random_string(string.ascii_lowercase, 8)
|
||||
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f'Im {self._name}')
|
1
tests/custom/di/src/tests/__init__.py
Normal file
1
tests/custom/di/src/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports:
|
Reference in New Issue
Block a user