|
1 | 1 | [](https://www.gnu.org/licenses/gpl-3.0.html) |
2 | 2 |  |
3 | | -[](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml) |
4 | 3 |
|
5 | 4 | ## py-dependency-injection |
6 | 5 |
|
7 | | -This is a dependency injection library for python. It provides a simple and flexible way to manage dependencies in your Python applications, promoting modularity, testability, and code maintainability. |
| 6 | +This is a simple and lightweight dependency injection library for python. |
8 | 7 |
|
9 | 8 | ### Features: |
10 | 9 |
|
11 | 10 | - Dependency Container |
| 11 | +- Dependency Scopes |
12 | 12 | - Constructor Injection |
13 | | -- Support Dependency Scopes |
| 13 | +- Method Injection |
14 | 14 |
|
15 | 15 | ### Python Compatibility |
16 | 16 |
|
17 | | -This library is compatible with the following Python versions: |
| 17 | +This library is compatible and tested with the following Python versions: |
18 | 18 |
|
19 | 19 | - Python 3.7 |
20 | 20 | - Python 3.8 |
21 | 21 | - Python 3.9 |
22 | 22 | - Python 3.10 |
23 | 23 | - Python 3.11 |
24 | 24 | - Python 3.12 |
| 25 | + |
| 26 | +[](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml) |
25 | 27 |
|
26 | 28 | ### Installation: |
27 | 29 |
|
28 | 30 | ```bash |
29 | 31 | $ pip install py-dependency-injection |
30 | 32 | ``` |
31 | 33 |
|
32 | | -### Example: |
| 34 | +### Example - Getting a dependency container: |
33 | 35 |
|
34 | 36 | ```python |
| 37 | +# While you can create multiple containers, it's typically recommended to use one per application. |
| 38 | +# For this purpose, you can easily obtain the default container. |
| 39 | + |
35 | 40 | from dependency_injection.container import DependencyContainer |
36 | 41 |
|
37 | 42 |
|
38 | | -container = DependencyContainer.get_instance() |
| 43 | +dependency_container = DependencyContainer.get_instance() |
| 44 | +``` |
| 45 | + |
| 46 | +### Example - Registering dependencies: |
| 47 | + |
| 48 | +```python |
| 49 | +# You can register using one of three scopes - transient, scoped or singleton. |
| 50 | + |
| 51 | +dependency_container.register_transient(SomeInterface, SomeClass) |
| 52 | +dependency_container.register_scoped(AnotherInterface, AnotherClass) |
| 53 | +dependency_container.register_singleton(ThirdInterface, ThirdClass) |
| 54 | +``` |
| 55 | + |
| 56 | +### Example - Resolving dependencies: |
| 57 | + |
| 58 | +```python |
| 59 | +# When resolving scoped dependencies, specify the scope explicitly if needed. |
| 60 | +# The scope, often associated with an application service action invocation, can be provided for scoped instances. |
| 61 | +# Default resolution is applied for non-scoped instances. |
| 62 | + |
| 63 | +transient_instance = dependency_container.resolve(SomeInterface) |
| 64 | +scoped_instance = dependency_container.resolve(AnotherInterface, scope_name="some-action-id") # application service action invocation ID |
| 65 | +singleton_instance = dependency_container.resolve(ThirdInterface) |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | +### Example - Constructor injection: |
| 70 | + |
| 71 | +```python |
| 72 | +# As long as the class is resolved using the dependency container, |
| 73 | +# dependencies are injected into it's constructor at resolution time. |
| 74 | + |
| 75 | +class Foo: |
| 76 | + |
| 77 | + def __init__(self, transient_instance: SomeInterface, scoped_instance: AnotherInterface, singleton_instance: ThirdInterface): |
| 78 | + self._transient_instance = transient_instance |
| 79 | + self._scoped_instance = scoped_instance |
| 80 | + self._singleton_instance = singleton_instance |
| 81 | +``` |
| 82 | + |
| 83 | +### Example - Method injection: |
39 | 84 |
|
40 | | -# Register dependencies |
41 | | -container.register_transient(SomeInterface, SomeClass) |
42 | | -container.register_scoped(AnotherInterface, AnotherClass) |
43 | | -container.register_singleton(ThirdInterface, ThirdClass) |
| 85 | +```python |
| 86 | +# Dependencies can be injected into an instance method using the `@inject` decorator. |
| 87 | +# The dependency container and scope name can be provided as arguments to the decorator. |
| 88 | +# If none are provided, the default container and scope are applied. |
| 89 | +from dependency_injection.decorator import inject |
44 | 90 |
|
45 | | -# Resolve dependencies |
46 | | -transient_instance = container.resolve(SomeInterface) |
47 | | -scoped_instance = container.resolve(AnotherInterface, scope_name="http_request_scope_123") |
48 | | -singleton_instance = container.resolve(ThirdInterface) |
49 | 91 |
|
50 | | -# Use dependencies |
51 | | -transient_instance.do_something() |
52 | | -scoped_instance.do_something() |
53 | | -singleton_instance.do_something() |
| 92 | +class Foo: |
54 | 93 |
|
| 94 | + @inject() |
| 95 | + def bar(self, transient_instance: SomeInterface, scoped_instance: AnotherInterface, singleton_instance: ThirdInterface): |
| 96 | + transient_instance.do_something() |
| 97 | + scoped_instance.do_something() |
| 98 | + singleton_instance.do_something() |
55 | 99 | ``` |
56 | 100 |
|
57 | 101 | ### Documentation: |
58 | 102 |
|
59 | | -You can find the latest [documentation](https://py-dependency-injection.readthedocs.io/en/latest/) at readthedocs. |
| 103 | +You can find the latest documentation at [readthedocs](https://py-dependency-injection.readthedocs.io/en/latest/). |
60 | 104 |
|
61 | 105 | ### Contribution: |
62 | 106 |
|
63 | 107 | If you want to contribute to the code base, create a pull request on the develop branch. |
64 | 108 |
|
65 | | -We follow the git flow model, documentation can be found here: |
66 | | -- [Official documentation](https://nvie.com/posts/a-successful-git-branching-model/) |
67 | | -- [Atlassian guide](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow) |
| 109 | +We follow the [git flow](https://nvie.com/posts/a-successful-git-branching-model/) branching model. |
68 | 110 |
|
69 | 111 | ### Release Notes |
70 | 112 |
|
71 | | -#### [1.0.0-alpha.2](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.2) (2024-03-xx) |
| 113 | +#### [1.0.0-alpha.2](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.2) (2024-02-xx) |
72 | 114 |
|
73 | 115 | - Python Version Support: Added support for Python versions 3.7, 3.9, 3.10, 3.11, and 3.12. |
| 116 | +- New Feature: Method Injection with Decorator: Introduced a new feature allowing method injection using the @inject decorator. Dependencies can now be injected into an instance method, providing more flexibility in managing dependencies within class instance methods. |
| 117 | +- Documentation Update: Expanded and improved the documentation to include details about the newly added method injection feature and additional usage examples. Users can refer to the latest documentation at readthedocs for comprehensive guidance. |
74 | 118 |
|
75 | 119 | #### [1.0.0-alpha.1](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.1) (2024-02-25) |
76 | 120 |
|
|
0 commit comments