-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dependencies_precedence_example.py
61 lines (47 loc) · 1.87 KB
/
dependencies_precedence_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
58
59
60
61
"""
In this example you'll see the use of declaring an injectable as primary and the use
of explicitly declared qualifiers.
We declare an abstract base class ``AbstractService`` which exposes an abstract
``combine`` method. The classes ``SumService`` and ``MultiplyService`` both implement
``AbstractService`` and are declared as injectable though we specify different
qualifiers for each and also declare ``SumService`` as primary.
Now, when we inject an ``AbstractService`` into our ``DependenciesPrecedence`` example
class we will get an instance of the ``SumService`` class since we declared it as
primary. We inject a ``MultipleService`` instance as well by using the explicit
qualifier attributed to it.
.. note::
If ``SumService`` wasn't declared as primary then injecting ``AbstractService``
would have failed and raised an error indicating there was ambiguity in resolving
the specified dependency.
.. seealso::
The :ref:`qualifier_overloading_example` shows how to get all instances which
resolves a dependency instead of just the primary one.
"""
# sphinx-start
from examples import Example
from examples.dependencies_precedence.abstract_service import AbstractService
from injectable import (
autowired,
Autowired,
load_injection_container,
)
class DependenciesPrecedence(Example):
@autowired
def __init__(
self,
abstract_service_1: Autowired(AbstractService),
abstract_service_2: Autowired("multiply"),
):
self.abstract_service_1 = abstract_service_1
self.abstract_service_2 = abstract_service_2
def run(self):
self.abstract_service_1.combine(4, 2)
# 4 + 2 = 6
self.abstract_service_2.combine(4, 2)
# 4 * 2 = 8
def run_example():
load_injection_container()
example = DependenciesPrecedence()
example.run()
if __name__ == "__main__":
run_example()