Skip to content

Commit 0dacedc

Browse files
committed
✨ Added function to inject dependencies
1 parent c89d3b6 commit 0dacedc

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

pymediator/mediator.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,48 @@ class BaseMediator(ABC):
1616
request to the handler.
1717
"""
1818

19-
def __init__(self, *, registry: Registry) -> None:
19+
def __init__(self, *, registry: Registry, dependencies: dict | None = None) -> None:
2020
self._registry: Registry = registry
21+
self._dependencies: dict = dependencies or {}
22+
23+
@property
24+
def registry(self) -> Registry:
25+
return self._registry
26+
27+
@abstractmethod
28+
def inject(self, **kwargs) -> "BaseMediator": # type: ignore[no-untyped-def]
29+
"""
30+
Abstract method to inject dependencies to the handler.
31+
32+
Args:
33+
kwargs (dict[str, Any]): The dependencies to be injected.
34+
35+
Returns:
36+
BaseMediator: A mediator instance ready to inject dependencies when making
37+
the handler call
38+
"""
2139

2240
@abstractmethod
23-
def send(self, request: Request, dependencies: dict[str, Any] | None = None) -> Any:
41+
def send(self, request: Request) -> Any:
2442
"""
2543
Abstract method to send the request to the handler.
2644
2745
Args:
2846
request (Request): The request to be handled.
2947
dependencies (dict[str, Any] | None, optional): A dictionary of dependencies
30-
to pass to the handler during execution.
48+
to pass to the handler during execution.
3149
3250
Returns:
3351
Any: The result from handling the request.
3452
"""
3553

3654

3755
class Mediator(BaseMediator):
38-
def send(self, request: Request, dependencies: dict[str, Any] | None = None) -> Any:
39-
handler: Handler = self._registry.get_handler(request)(**(dependencies or {}))
56+
def inject(self, **kwargs) -> "Mediator": # type: ignore[no-untyped-def]
57+
return Mediator(registry=self.registry, dependencies=kwargs)
58+
59+
def send(self, request: Request) -> Any:
60+
handler: Handler = self._registry.get_handler(request)(**self._dependencies)
4061
if not isinstance(handler, Handler):
4162
raise HandlerProtocolViolationException(handler.__class__.__name__)
4263
return handler.handle(request)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pymediator"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "Lightweight and extensible implementation of the Mediator pattern in Python"
55
authors = ["Sergio Torrado"]
66
license = "MIT"

tests/test_mediators.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,26 @@ def sum(self, request: SumRequest) -> int:
3737
mediator: Mediator = Mediator(registry=registry)
3838
with pytest.raises(HandlerProtocolViolationException):
3939
mediator.send(SumRequest(x=3, y=5))
40+
41+
def test_injection(self) -> None:
42+
class SumUtils:
43+
def sum(self, x: int, y: int) -> int:
44+
return x + y
45+
46+
@dataclass
47+
class SumRequest:
48+
x: int
49+
y: int
50+
51+
class SumHandler:
52+
def __init__(self, sum_utils: SumUtils) -> None:
53+
self._sum_utils: SumUtils = sum_utils
54+
55+
def handle(self, request: SumRequest) -> int:
56+
return self._sum_utils.sum(request.x, request.y)
57+
58+
registry: InstanceRegistry = InstanceRegistry()
59+
registry.register(SumRequest, SumHandler)
60+
mediator: Mediator = Mediator(registry=registry)
61+
response: int = mediator.inject(sum_utils=SumUtils()).send(SumRequest(x=3, y=5))
62+
assert response == 8

0 commit comments

Comments
 (0)