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
+45-45Lines changed: 45 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
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.
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 services in different contexts or areas of your application.
6
6
7
7
.. code-block:: python
8
8
@@ -17,47 +17,47 @@ In this example, we demonstrate how to create and retrieve dependency containers
17
17
)
18
18
19
19
20
-
####################################
21
-
Registering dependencies with scopes
22
-
####################################
20
+
################################
21
+
Registering services with scopes
22
+
################################
23
23
24
-
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.
24
+
This example shows how to register services with different scopes (transient, scoped, and singleton). This is important for controlling the lifecycle and reuse of your services.
25
25
26
26
.. code-block:: python
27
27
28
-
# Register a transient dependency
28
+
# Register a transient service
29
29
dependency_container.register_transient(
30
30
Connection,
31
31
PostgresConnection
32
32
)
33
33
34
-
# Register a scoped dependency
34
+
# Register a scoped service
35
35
dependency_container.register_scoped(
36
36
Connection,
37
37
PostgresConnection,
38
38
scope_name="http_request"
39
39
)
40
40
41
-
# Register a singleton dependency
41
+
# Register a singleton service
42
42
dependency_container.register_singleton(
43
43
Connection,
44
44
PostgresConnection
45
45
)
46
46
47
47
48
-
######################################
49
-
Registering with constructor arguments
50
-
######################################
48
+
###################################
49
+
Registering with constructor kwargs
50
+
###################################
51
51
52
-
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.
52
+
Here, we illustrate how to register a service with constructor keyword arguments. This allows you to provide specific values or configurations to your services when they are instantiated.
53
53
54
54
.. code-block:: python
55
55
56
-
# Register with constructor arguments
56
+
# Register with constructor kwargs
57
57
dependency_container.register_transient(
58
58
Connection,
59
59
PostgresConnection,
60
-
constructor_args={
60
+
constructor_kwargs={
61
61
"host": "localhost",
62
62
"port": 5432
63
63
}
@@ -68,7 +68,7 @@ Here, we illustrate how to register a dependency with constructor arguments. Thi
68
68
Resolving with factory functions
69
69
################################
70
70
71
-
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.
71
+
In this section, we demonstrate how to register and resolve services using the factory pattern. This provides flexibility in how your services are created and configured. You can use factory functions, factory classes and factory lambdas.
72
72
73
73
.. note::
74
74
Any `callable <https://docs.python.org/3/glossary.html#term-callable>`_ can be used as factory.
@@ -86,7 +86,7 @@ In this section, we demonstrate how to register and resolve dependencies using t
86
86
dependency_container.register_factory(
87
87
Connection,
88
88
factory_function,
89
-
factory_args={
89
+
factory_kwargs={
90
90
"host": "localhost",
91
91
"port": 5432
92
92
}
@@ -107,7 +107,7 @@ In this section, we demonstrate how to register and resolve dependencies using t
107
107
dependency_container.register_factory(
108
108
Connection,
109
109
FactoryClass.create,
110
-
factory_args={
110
+
factory_kwargs={
111
111
"host": "localhost",
112
112
"port": 5432
113
113
}
@@ -122,7 +122,7 @@ In this section, we demonstrate how to register and resolve dependencies using t
122
122
host=host,
123
123
port=port
124
124
),
125
-
factory_args={
125
+
factory_kwargs={
126
126
"host": "localhost",
127
127
"port": 5432
128
128
}
@@ -133,7 +133,7 @@ In this section, we demonstrate how to register and resolve dependencies using t
133
133
Registering and using instances
134
134
###############################
135
135
136
-
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.
136
+
This example demonstrates how to register and use instances of your services. This is useful when you want to provide a specific instance of a service for use throughout your application.
137
137
138
138
.. code-block:: python
139
139
@@ -150,15 +150,15 @@ This example demonstrates how to register and use instances of your dependencies
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.
161
+
In this example, we show how to register and resolve services using tags. This allows you to categorize and retrieve specific groups of services based on their tags.
162
162
163
163
.. code-block:: python
164
164
@@ -172,7 +172,7 @@ In this example, we show how to register and resolve dependencies using tags. Th
172
172
}
173
173
)
174
174
175
-
# Register another dependency with tags
175
+
# Register another service with tags
176
176
dependency_container.register_scoped(
177
177
BusConnection,
178
178
KafkaBusConnection,
@@ -182,31 +182,31 @@ In this example, we show how to register and resolve dependencies using tags. Th
182
182
}
183
183
)
184
184
185
-
# Resolve all dependencies with the 'Startable' tag
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.
201
+
This example illustrates how to use constructor injection to automatically inject services 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 services in your software application.
202
202
203
203
.. code-block:: python
204
204
205
205
classOrderRepository:
206
206
def__init__(self, connection: Connection):
207
207
self.connection = connection
208
208
209
-
# Register dependencies
209
+
# Register services
210
210
dependency_container.register_transient(
211
211
OrderRepository
212
212
)
@@ -216,20 +216,20 @@ This example illustrates how to use constructor injection to automatically injec
This example demonstrates how to use constructor injection to automatically inject tagged dependencies into your classes. By leveraging tags, you can group and categorize dependencies, enabling automatic injection based on specific criteria.
232
+
This example demonstrates how to use constructor injection to automatically inject tagged services into your classes. By leveraging tags, you can group and categorize services, enabling automatic injection based on specific criteria.
233
233
234
234
.. code-block:: python
235
235
@@ -250,36 +250,36 @@ This example demonstrates how to use constructor injection to automatically inje
print(f"Primary port instance: {type(application.primary_ports[0]).__name__}") # Output: HttpAdapter
267
267
print(f"Secondary port instance: {type(application.secondary_ports[0]).__name__}") # Output: PostgresCarRepository
268
268
269
269
270
-
In this example, the `Application` class expects lists of instances tagged with `PrimaryPort` and `SecondaryPort`. By tagging and registering these dependencies, the container automatically injects the correct instances into the `Application` class when it is resolved.
270
+
In this example, the `Application` class expects lists of instances tagged with `PrimaryPort` and `SecondaryPort`. By tagging and registering these services, the container automatically injects the correct instances into the `Application` class when it is resolved.
271
271
272
-
Tags offer a powerful way to manage dependencies, ensuring that the right instances are injected based on your application's needs.
272
+
Tags offer a powerful way to manage services, ensuring that the right instances are injected based on your application's needs.
273
273
274
274
.. note::
275
-
You can also use the ``AnyTagged`` and ``AllTagged`` classes to inject dependencies based on more complex tagging logic. ``AnyTagged`` allows injection of any dependency matching one or more specified tags, while ``AllTagged`` requires the dependency to match all specified tags before injection. This provides additional flexibility in managing and resolving dependencies in your application.
275
+
You can also use the ``AnyTagged`` and ``AllTagged`` classes to inject services based on more complex tagging logic. ``AnyTagged`` allows injection of any service matching one or more specified tags, while ``AllTagged`` requires the service to match all specified tags before injection. This provides additional flexibility in managing and resolving services in your application.
276
276
277
277
278
278
######################
279
279
Using method injection
280
280
######################
281
281
282
-
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.
282
+
This example demonstrates how to use method injection to inject services into methods at runtime. This is useful for dynamically providing services to class- or static methods, without affecting the entire class.
283
283
284
284
.. note::
285
285
You can pass the arguments ``container_name`` and ``scope_name`` to ``@inject``.
@@ -296,7 +296,7 @@ This example demonstrates how to use method injection to inject dependencies int
Copy file name to clipboardExpand all lines: docs/userguide.rst
+21-12Lines changed: 21 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,15 @@ Introduction
14
14
15
15
This guide provides an overview of the key concepts and demonstrates how to start using the library effectively. For detailed examples, see the `Examples` section.
16
16
17
+
###########
18
+
Terminology
19
+
###########
20
+
21
+
In this guide:
22
+
23
+
- **Service** — an abstract type or protocol you depend on.
24
+
- **Implementation** — a concrete class that fulfills the service.
25
+
17
26
############
18
27
Installation
19
28
############
@@ -46,10 +55,10 @@ Core Concepts and Features
46
55
47
56
`py-dependency-injection` offers:
48
57
49
-
- **Scoped Dependency Management**: Define lifetimes for your dependencies (e.g., transient, scoped, singleton).
50
-
- **Flexible Registrations**: Use constructors, factories, or predefined instances for dependency registration.
51
-
- **Tag-Based Organization**: Categorize and resolve dependencies using tags.
52
-
- **Multiple Containers**: Isolate dependencies for different parts of your application.
58
+
- **Scoped Service Management**: Define lifetimes for your services (transient, scoped, singleton).
59
+
- **Flexible Registrations**: Use constructors, factories, or predefined instances for service registration.
60
+
- **Tag-Based Organization**: Categorize and resolve services using tags.
61
+
- **Multiple Containers**: Isolate services for different parts of your application.
53
62
54
63
Refer to the `Examples` section for detailed usage scenarios.
55
64
@@ -58,13 +67,13 @@ Quick Start Overview
58
67
####################
59
68
60
69
1. **Create a Dependency Container**:
61
-
- The `DependencyContainer` is the core object for managing dependencies.
70
+
- The `DependencyContainer` is the core object for registering and resolving services.
62
71
63
-
2. **Register Dependencies**:
64
-
- Dependencies can be registered with different lifetimes: transient, scoped, or singleton.
72
+
2. **Register Services**:
73
+
- Services can be registered with different lifetimes: transient, scoped, or singleton.
65
74
66
-
3. **Resolve Dependencies**:
67
-
- Use the container to resolve dependencies where needed.
75
+
3. **Resolve Services**:
76
+
- Use the container to resolve services where needed.
0 commit comments