Skip to content

Commit 6de4d93

Browse files
committed
Add example on using tagged dependencies in constructor injection.
1 parent 55f01cc commit 6de4d93

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/examples.rst

+50
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,56 @@ This example illustrates how to use constructor injection to automatically injec
225225
print(repository.connection.__class__.__name__) # Output: PostgresConnection
226226
227227
228+
######################################################
229+
Using constructor injection with tagged dependencies
230+
######################################################
231+
232+
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.
233+
234+
.. code-block:: python
235+
236+
class PrimaryPort:
237+
pass
238+
239+
class SecondaryPort:
240+
pass
241+
242+
class HttpAdapter(PrimaryPort):
243+
pass
244+
245+
class PostgresCarRepository(SecondaryPort):
246+
pass
247+
248+
class Application:
249+
def __init__(self, primary_ports: List[Tagged[PrimaryPort]], secondary_ports: List[Tagged[SecondaryPort]]):
250+
self.primary_ports = primary_ports
251+
self.secondary_ports = secondary_ports
252+
253+
# Register dependencies with tags
254+
dependency_container.register_transient(HttpAdapter, tags={PrimaryPort})
255+
dependency_container.register_transient(PostgresCarRepository, tags={SecondaryPort})
256+
257+
# Register the Application class to have its dependencies injected
258+
dependency_container.register_transient(Application)
259+
260+
# Resolve the Application class, with tagged dependencies automatically injected
261+
application = dependency_container.resolve(Application)
262+
263+
# Use the injected dependencies
264+
print(f"Primary ports: {len(application.primary_ports)}") # Output: Primary ports: 1
265+
print(f"Secondary ports: {len(application.secondary_ports)}") # Output: Secondary ports: 1
266+
print(f"Primary port instance: {type(application.primary_ports[0]).__name__}") # Output: HttpAdapter
267+
print(f"Secondary port instance: {type(application.secondary_ports[0]).__name__}") # Output: PostgresCarRepository
268+
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.
271+
272+
Tags offer a powerful way to manage dependencies, ensuring that the right instances are injected based on your application's needs.
273+
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.
276+
277+
228278
######################
229279
Using method injection
230280
######################

0 commit comments

Comments
 (0)