@@ -10,9 +10,9 @@ A new instance is created every time the dependency is resolved.
1010``` python
1111from injection import injectable
1212
13+
1314@injectable
14- class Dependency :
15- ...
15+ class Dependency : ...
1616```
1717
1818## Singleton
@@ -21,9 +21,9 @@ A single instance is created and shared across the entire application.
2121``` python
2222from injection import singleton
2323
24+
2425@singleton
25- class Dependency :
26- ...
26+ class Dependency : ...
2727```
2828
2929## Constant
@@ -33,10 +33,12 @@ Register a pre-existing value as a dependency.
3333from dataclasses import dataclass
3434from injection import set_constant
3535
36+
3637@dataclass (frozen = True )
3738class Settings :
3839 api_key: str
3940
41+
4042settings = set_constant(Settings(" <secret_api_key>" ))
4143```
4244
@@ -53,9 +55,9 @@ For lazy constants, use the `@constant` decorator:
5355``` python
5456from injection import constant
5557
58+
5659@constant
57- class LazySettings :
58- ...
60+ class LazySettings : ...
5961```
6062
6163## Factories
@@ -66,8 +68,9 @@ _Make sure not to forget the return type annotation._
6668``` python
6769from injection import injectable
6870
69- class Dependency :
70- ...
71+
72+ class Dependency : ...
73+
7174
7275@injectable
7376def _dependency_factory () -> Dependency:
@@ -85,12 +88,12 @@ Register an implementation for an abstract class or protocol.
8588from injection import injectable
8689from abc import ABC
8790
88- class AbstractDependency (ABC ):
89- ...
91+
92+ class AbstractDependency (ABC ): ...
93+
9094
9195@injectable (on = AbstractDependency)
92- class Dependency (AbstractDependency ):
93- ...
96+ class Dependency (AbstractDependency ): ...
9497```
9598
9699## Scoped
@@ -99,9 +102,9 @@ A single instance is created per scope. Using a `StrEnum` for scope names is rec
99102``` python
100103from injection import scoped
101104
105+
102106@scoped (" <scope_name>" )
103- class Dependency :
104- ...
107+ class Dependency : ...
105108```
106109
107110## Scoped with context manager
@@ -114,12 +117,12 @@ Scoped dependencies can be registered using generator functions (sync or async)
114117from collections.abc import Iterator
115118from injection import scoped
116119
120+
117121class Dependency :
118- def open (self ):
119- ...
122+ def open (self ): ...
123+
124+ def close (self ): ...
120125
121- def close (self ):
122- ...
123126
124127@scoped (" <scope_name>" )
125128def dependency_factory () -> Iterator[Dependency]:
@@ -137,7 +140,7 @@ Register a dependency for a specific profile. Using a `StrEnum` for profile name
137140``` python
138141from injection import mod
139142
143+
140144@mod (" <profile_name>" ).injectable
141- class Dependency :
142- ...
145+ class Dependency : ...
143146```
0 commit comments