-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f54eb45
commit 3befb0e
Showing
4 changed files
with
54 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Auto injection | ||
|
||
**Auto injection** allows you to **not specify markers and providers** | ||
in the parameters of the function for which you want to inject dependencies. | ||
|
||
Here it is enough to **specify** the **types** for which the corresponding providers | ||
have already been created in the container. | ||
`@autoinject` decorator will try to find providers according to the types | ||
of the function parameters and inject the dependencies. | ||
|
||
### Example | ||
|
||
```python | ||
from injection import DeclarativeContainer, auto_inject, providers | ||
|
||
|
||
class SomeClass: | ||
def __init__(self, a: int, b: str) -> None: | ||
self.a = a | ||
self.b = b | ||
|
||
def func(self) -> str: | ||
return "sync func" | ||
|
||
async def async_func(self) -> str: | ||
return "async func" | ||
|
||
|
||
class DIContainer(DeclarativeContainer): | ||
some_provider = providers.Factory(SomeClass, a=1, b="b") | ||
|
||
|
||
def test_auto_inject_to_sync_function() -> None: | ||
@auto_inject(target_container=DIContainer) | ||
def _sync_func(obj: SomeClass) -> None: | ||
assert obj.func() == "sync func" | ||
assert obj.a == 1 | ||
assert obj.b == "b" | ||
|
||
_sync_func() | ||
|
||
|
||
async def test_auto_inject_to_async_function() -> None: | ||
@auto_inject(target_container=DIContainer) | ||
async def _async_func(obj: SomeClass) -> None: | ||
assert await obj.async_func() == "async func" | ||
assert obj.a == 1 | ||
assert obj.b == "b" | ||
|
||
await _async_func() | ||
``` |
This file was deleted.
Oops, something went wrong.