Skip to content

Commit c714857

Browse files
committed
Update examples in README.
1 parent 0915886 commit c714857

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

README.md

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,120 @@
11
[![License](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.html)
22
![First Principles Software](https://img.shields.io/badge/Powered_by-First_Principles_Software-blue)
3-
[![Push on master](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml)
43

54
## py-dependency-injection
65

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.
87

98
### Features:
109

1110
- Dependency Container
11+
- Dependency Scopes
1212
- Constructor Injection
13-
- Support Dependency Scopes
13+
- Method Injection
1414

1515
### Python Compatibility
1616

17-
This library is compatible with the following Python versions:
17+
This library is compatible and tested with the following Python versions:
1818

1919
- Python 3.7
2020
- Python 3.8
2121
- Python 3.9
2222
- Python 3.10
2323
- Python 3.11
2424
- Python 3.12
25+
26+
[![Push on master](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml)
2527

2628
### Installation:
2729

2830
```bash
2931
$ pip install py-dependency-injection
3032
```
3133

32-
### Example:
34+
### Example - Getting a dependency container:
3335

3436
```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+
3540
from dependency_injection.container import DependencyContainer
3641

3742

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:
3984

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
4490

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)
4991

50-
# Use dependencies
51-
transient_instance.do_something()
52-
scoped_instance.do_something()
53-
singleton_instance.do_something()
92+
class Foo:
5493

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()
5599
```
56100

57101
### Documentation:
58102

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/).
60104

61105
### Contribution:
62106

63107
If you want to contribute to the code base, create a pull request on the develop branch.
64108

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.
68110

69111
### Release Notes
70112

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)
72114

73115
- 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.
74118

75119
#### [1.0.0-alpha.1](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.1) (2024-02-25)
76120

docs/versionhistory.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
Version history
33
###############
44

5-
**1.0.0-alpha.2 (2024-03-xx)**
5+
**1.0.0-alpha.2 (2024-02-xx)**
66

77
- Python Version Support: Added support for Python versions 3.7, 3.9, 3.10, 3.11, and 3.12.
8+
- 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.
9+
- 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.
810

911
`View release on GitHub <https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.2>`_
1012

0 commit comments

Comments
 (0)