File tree Expand file tree Collapse file tree 4 files changed +72
-25
lines changed Expand file tree Collapse file tree 4 files changed +72
-25
lines changed Original file line number Diff line number Diff line change 99 introduction/ioc-container
1010 introduction/fastapi
1111 introduction/litestar
12- introduction/multiple-containers
1312 introduction/inject-factories
13+ introduction/multiple-containers
14+ introduction/dynamic-container
1415
1516.. toctree::
1617 :maxdepth: 1
Original file line number Diff line number Diff line change 1+ # Dynamic container
2+
3+ You can dynamically assign providers to container:
4+ ``` python
5+ import datetime
6+
7+ from tests import container
8+ from that_depends import BaseContainer, providers
9+
10+
11+ class DIContainer (BaseContainer ):
12+ sync_resource: providers.Resource[datetime.datetime]
13+ async_resource: providers.AsyncResource[datetime.datetime]
14+
15+
16+ DIContainer.sync_resource = providers.Resource(container.create_sync_resource)
17+ DIContainer.async_resource = providers.AsyncResource(container.create_async_resource)
18+ ```
19+
20+ And than you can use these providers as usual:
21+
22+ ``` python
23+ sync_resource = await DIContainer.sync_resource()
24+ async_resource = await DIContainer.async_resource()
25+ ```
Original file line number Diff line number Diff line change 1+ import datetime
2+
3+ from tests import container
4+ from that_depends import BaseContainer , providers
5+
6+
7+ class DIContainer (BaseContainer ):
8+ sync_resource : providers .Resource [datetime .datetime ]
9+ async_resource : providers .AsyncResource [datetime .datetime ]
10+
11+
12+ DIContainer .sync_resource = providers .Resource (container .create_sync_resource )
13+ DIContainer .async_resource = providers .AsyncResource (container .create_async_resource )
14+
15+
16+ async def test_dynamic_container () -> None :
17+ sync_resource = await DIContainer .sync_resource ()
18+ async_resource = await DIContainer .async_resource ()
19+
20+ assert isinstance (sync_resource , datetime .datetime )
21+ assert isinstance (async_resource , datetime .datetime )
You can’t perform that action at this time.
0 commit comments