-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend singleton and provided instance docs
- Loading branch information
ivan
committed
Aug 3, 2024
1 parent
c156143
commit 4c97347
Showing
2 changed files
with
71 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,46 @@ | ||
# Provided instance | ||
|
||
soon... | ||
You can pass provider field values to other providers, | ||
which will provide dependency injection. | ||
To access the values of an object's fields inside a provider, | ||
you need to use the `provided` property of the provider. | ||
|
||
This is useful in cases when your entities depend on external data | ||
from configuration files or environment variables. | ||
You need to parse this external data and save it into a class or dataclass, | ||
and the container will inject this data. | ||
|
||
### Example | ||
```python3 | ||
from dataclasses import dataclass | ||
|
||
from injection import DeclarativeContainer, providers | ||
|
||
|
||
@dataclass | ||
class Settings: | ||
env_1: str | ||
env_2: int | ||
|
||
|
||
class SomeService: | ||
def __init__(self, env_1: str, env_2: int): | ||
self.env_1 = env_1 | ||
self.env_2 = env_2 | ||
|
||
|
||
class Container(DeclarativeContainer): | ||
settings = providers.Singleton(Settings, env_1="value", env_2=193) | ||
|
||
service = providers.Transient( | ||
SomeService, | ||
env_1=settings.provided.env_1, | ||
env_2=settings.provided.env_2, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
resolved_service = Container.service() | ||
assert resolved_service.env_1 == "value" | ||
assert resolved_service.env_2 == 193 | ||
``` |
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