-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
qualifier_overloading_example.py
57 lines (43 loc) · 2.05 KB
/
qualifier_overloading_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
In this example you'll learn about overloading qualifiers/classes for injection and how
to take advantage of that to inject multiple dependencies as a list of instances.
Overloading happens when two or more injectables are declared for a same qualifier
or class.
In this example we create a abstract base class ``SenderService`` and implement it in
other three classes, ``EmailSenderService``, ``SmsSenderService``, and
``FaxSenderService``. All the three concrete services are declared as injectables and
as injection declared class propagates to base classes we end up with three injectables
declared for the ``SenderService`` class.
In our ``QualifierOverloading`` example class we inject a list with all injectables
declared for the ``SenderService`` by using the :class:`typing.List` type. We also use
the ``exclude_groups`` parameter to filter out injectables that were declared with the
``"old"`` group label.
.. seealso::
The :ref:`dependencies_precedence_example` shows how dependency resolution works in
regards to precedence when a qualifier or class are resolved by multiple
injectables and you're injecting a single instance and not all matching injectables.
"""
# sphinx-start
from typing import List
from examples import Example
from examples.qualifier_overloading.sender_service import SenderService
from injectable import autowired, Autowired, load_injection_container
class QualifierOverloading(Example):
@autowired
def __init__(
self, sender_services: Autowired(List[SenderService], exclude_groups=["old"]),
):
self.sender_services = sender_services
def send_message(self, message: str, recipient: str):
for sender_service in self.sender_services:
sender_service.send(message, recipient)
def run(self):
self.send_message(message="Hello!", recipient="World")
# Sending Email to World: Hello!
# Sending SMS to World: Hello!
def run_example():
load_injection_container()
example = QualifierOverloading()
example.run()
if __name__ == "__main__":
run_example()