Skip to content

Commit

Permalink
dynamic container: tests and docs (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnik512 authored Jun 21, 2024
1 parent 3889e72 commit 63a5dda
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
introduction/ioc-container
introduction/fastapi
introduction/litestar
introduction/multiple-containers
introduction/inject-factories
introduction/multiple-containers
introduction/dynamic-container
.. toctree::
:maxdepth: 1
Expand Down
25 changes: 25 additions & 0 deletions docs/introduction/dynamic-container.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Dynamic container

You can dynamically assign providers to container:
```python
import datetime

from tests import container
from that_depends import BaseContainer, providers


class DIContainer(BaseContainer):
sync_resource: providers.Resource[datetime.datetime]
async_resource: providers.AsyncResource[datetime.datetime]


DIContainer.sync_resource = providers.Resource(container.create_sync_resource)
DIContainer.async_resource = providers.AsyncResource(container.create_async_resource)
```

And than you can use these providers as usual:

```python
sync_resource = await DIContainer.sync_resource()
async_resource = await DIContainer.async_resource()
```
48 changes: 24 additions & 24 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/test_dynamic_container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import datetime

from tests import container
from that_depends import BaseContainer, providers


class DIContainer(BaseContainer):
sync_resource: providers.Resource[datetime.datetime]
async_resource: providers.AsyncResource[datetime.datetime]


DIContainer.sync_resource = providers.Resource(container.create_sync_resource)
DIContainer.async_resource = providers.AsyncResource(container.create_async_resource)


async def test_dynamic_container() -> None:
sync_resource = await DIContainer.sync_resource()
async_resource = await DIContainer.async_resource()

assert isinstance(sync_resource, datetime.datetime)
assert isinstance(async_resource, datetime.datetime)

0 comments on commit 63a5dda

Please sign in to comment.