@@ -6,21 +6,52 @@ A simple Python dependency injection framework.
66
77** This project is under active development. The following example does not represent the final state for the project.**
88
9+ The injection framework is configured to inject any default values for method arguments that are instances
10+ of ` providers.Provider ` .
11+
12+ This implementation works by wrapping decorators around methods any patching any unfilled ` providers.Provider ` default
13+ arguments at runtime.
14+
15+ All dependency injection is lazily evaluated so providers are only evaluated when a method is called. This approach is
16+ optimal as it reduces necessary computation for expensive services and reduces
17+
18+ ### Decorator Injection
19+
20+ With this approach you can automatically inject functions at load time using the ` @wiring.injected ` decorator.
21+
922``` python
1023from pif import wiring, providers
1124
1225
13- @wiring.injected
26+ @wiring.injected # <- automatically injects providers.Provider default arguments!
1427def my_function (a : str = providers.Singleton[str ](lambda : " hello world" )):
1528 return a
1629
1730
18- if __name__ == ' __main__' :
31+ if __name__ == " __main__" :
32+ assert " hello world" == my_function()
33+ ```
34+
35+ ### Module Injection
36+
37+ With this approach you can wire all methods in the specified modules.
38+
39+ ``` python
40+ from pif import wiring, providers
41+
42+
43+ def my_function (a : str = providers.Singleton[str ](lambda : " hello world" )):
44+ return a
45+
46+
47+ if __name__ == " __main__" :
48+ wiring.wire([__name__ ]) # <- dynamically inject methods with providers.Provider default arguments!
49+
1950 assert " hello world" == my_function()
2051```
2152
2253## Authors
2354
24- | [ ![ Zac Scott] ( https://avatars.githubusercontent.com/u/38968222 )] ( https://github.com/scottzach1 ) |
25- | :------------------------------------------------------------------------------------------------|
26- | [ Zac Scott (scottzach1)] ( https://github.com/scottzach1 ) |
55+ | [ ![ Zac Scott] ( https://avatars.githubusercontent.com/u/38968222?s=128&v=4 )] ( https://github.com/scottzach1 ) |
56+ | :---------------------------------------------------------------------------------------------------------- |
57+ | [ Zac Scott (scottzach1)] ( https://github.com/scottzach1 ) |
0 commit comments