You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/examples.rst
+24-5
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
Creating dependency containers
3
3
##############################
4
4
5
+
In this example, we demonstrate how to create and retrieve dependency containers using the `DependencyContainer` class. This is useful when you want to manage dependencies in different contexts or areas of your application.
6
+
5
7
.. code-block:: python
6
8
7
9
# Get the default dependency container
@@ -17,6 +19,8 @@ Creating dependency containers
17
19
Registering dependencies with scopes
18
20
####################################
19
21
22
+
This example shows how to register dependencies with different scopes (transient, scoped, and singleton). This is important for controlling the lifecycle and reuse of your dependencies.
23
+
20
24
.. code-block:: python
21
25
22
26
# Register a transient dependency
@@ -43,6 +47,8 @@ Registering dependencies with scopes
43
47
Registering with constructor arguments
44
48
######################################
45
49
50
+
Here, we illustrate how to register a dependency with constructor arguments. This allows you to provide specific values or configurations to your dependencies when they are instantiated.
51
+
46
52
.. code-block:: python
47
53
48
54
# Register with constructor arguments
@@ -60,8 +66,10 @@ Registering with constructor arguments
60
66
Resolving with factory functions
61
67
################################
62
68
69
+
In this section, we demonstrate how to register and resolve dependencies using the factory pattern. This provides flexibility in how your dependencies are created and configured. You can use factory functions, factory classes and factory lambdas.
70
+
63
71
.. note::
64
-
Any `callable <https://docs.python.org/3/glossary.html#term-callable>`_ can be used as factory function.
72
+
Any `callable <https://docs.python.org/3/glossary.html#term-callable>`_ can be used as factory.
65
73
66
74
.. code-block:: python
67
75
@@ -123,6 +131,8 @@ Resolving with factory functions
123
131
Registering and using instances
124
132
###############################
125
133
134
+
This example demonstrates how to register and use instances of your dependencies. This is useful when you want to provide a specific instance of a dependency for use throughout your application.
135
+
126
136
.. code-block:: python
127
137
128
138
# Create instance
@@ -146,6 +156,8 @@ Registering and using instances
146
156
Registering and resolving with tags
147
157
###################################
148
158
159
+
In this example, we show how to register and resolve dependencies using tags. This allows you to categorize and retrieve specific groups of dependencies based on their tags.
160
+
149
161
.. code-block:: python
150
162
151
163
# Register with tags
@@ -168,7 +180,7 @@ Registering and resolving with tags
168
180
}
169
181
)
170
182
171
-
# Resolve all dependencies with a specific tag
183
+
# Resolve all dependencies with the 'Startable' tag
@@ -184,6 +196,8 @@ Registering and resolving with tags
184
196
Using constructor injection
185
197
###########################
186
198
199
+
This example illustrates how to use constructor injection to automatically inject dependencies into your classes. This is a common pattern for managing dependencies in object-oriented programming. This is probably how you'll want to resolve 99% of the dependencies in your software application.
200
+
187
201
.. code-block:: python
188
202
189
203
classOrderRepository:
@@ -213,6 +227,14 @@ Using constructor injection
213
227
Using method injection
214
228
######################
215
229
230
+
This example demonstrates how to use method injection to inject dependencies into methods at runtime. This is useful for dynamically providing dependencies to class- or static methods, without affecting the entire class.
231
+
232
+
.. note::
233
+
You can pass the arguments ``container_name`` and ``scope_name`` to ``@inject``.
234
+
235
+
.. note::
236
+
The ``@inject`` has to be applied to the function after the ``@classmethod`` or ``@staticmethod``.
237
+
216
238
.. code-block:: python
217
239
218
240
classOrderController:
@@ -236,6 +258,3 @@ Using method injection
236
258
OrderController.place_order(
237
259
order=Order.create()
238
260
)
239
-
240
-
.. note::
241
-
You can pass ``container_name`` and ``scope_name`` arguments to the ``@inject`` decorator to specify container and/or scope. If none of the arguments are passed, the `default container` and the `default scope` will be used.
This library is currently in the alpha stage of development. Expect changes and improvements as we work towards a stable release.
4
+
1
5
py-dependency-injection
2
6
=======================
3
7
@@ -15,28 +19,12 @@ Key Advantages
15
19
16
20
- **Suitable for Learning:** Ideal for beginners exploring the concept of dependency injection.
17
21
- **Base Implementation for Frameworks:** Can be used as a foundational base for frameworks requiring a more specialized interface for dependency injection.
18
-
- **Standalone Solution:** Can be used on its own as a dependency injection solution in any software project.
19
-
20
-
Features
21
-
--------
22
-
23
-
- Dependency Containers
24
-
- Dependency Scopes
25
-
- Constructor Injection
26
-
- Method Injection
27
-
- Factory Registration
28
-
- Instance Registration
29
-
- Tag-Based Resolution
30
-
31
-
Documentation
32
-
-------------
33
-
34
-
For a detailed guide on how to use `py-dependency-injection`, please refer to the examples.
22
+
- **Standalone Solution:** Can also be used on its own, as a fully-featured dependency injection solution in any software project.
35
23
36
24
.. userguide-docs:
37
25
.. toctree::
38
26
:maxdepth:1
39
-
:caption:User guide
27
+
:caption:User Guide
40
28
41
29
userguide
42
30
@@ -47,12 +35,12 @@ For a detailed guide on how to use `py-dependency-injection`, please refer to th
47
35
48
36
examples
49
37
50
-
..versionhistory-docs:
38
+
..releases-docs:
51
39
.. toctree::
52
40
:maxdepth:1
53
41
:caption:Releases
54
42
55
-
versionhistory
43
+
releases
56
44
57
45
.. apireference-docs:
58
46
.. toctree::
@@ -61,4 +49,4 @@ For a detailed guide on how to use `py-dependency-injection`, please refer to th
61
49
62
50
py-modindex
63
51
64
-
You can find the source code for `py-dependency-injection` on `GitHub <https://github.com/runemalm/py-dependency-injection>`_.
52
+
You can find the source code for `py-dependency-injection` in our `GitHub repository<https://github.com/runemalm/py-dependency-injection>`_.
0 commit comments