-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from mardiros/features/dependency-injection-in…
…stanciation Refactor to isolate dependencies per transaction
- Loading branch information
Showing
11 changed files
with
226 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from collections.abc import Mapping, Sequence | ||
from typing import Any, Generic | ||
|
||
from messagebus.domain.model.message import TMessage | ||
from messagebus.service._async.unit_of_work import TAsyncUow | ||
from messagebus.typing import AsyncMessageHandler, P | ||
|
||
|
||
class AsyncDependency: | ||
"""Describe an async dependency""" | ||
|
||
async def on_after_commit(self) -> None: | ||
"""Method called when the unit of work transaction is has been commited.""" | ||
|
||
async def on_after_rollback(self) -> None: | ||
"""Method called when the unit of work transaction is has been rolled back.""" | ||
|
||
|
||
class AsyncMessageHook(Generic[TMessage, TAsyncUow, P]): | ||
callback: AsyncMessageHandler[TMessage, "TAsyncUow", P] | ||
dependencies: Sequence[str] | ||
|
||
def __init__( | ||
self, | ||
callback: AsyncMessageHandler[TMessage, "TAsyncUow", P], | ||
dependencies: Sequence[str], | ||
) -> None: | ||
self.callback = callback | ||
self.dependencies = dependencies | ||
|
||
async def __call__( | ||
self, | ||
msg: TMessage, | ||
uow: "TAsyncUow", | ||
dependencies: Mapping[str, AsyncDependency], | ||
) -> Any: | ||
deps = {k: dependencies[k] for k in self.dependencies} | ||
resp = await self.callback(msg, uow, **deps) # type: ignore | ||
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from collections.abc import Mapping, Sequence | ||
from typing import Any, Generic | ||
|
||
from messagebus.domain.model.message import TMessage | ||
from messagebus.service._sync.unit_of_work import TSyncUow | ||
from messagebus.typing import P, SyncMessageHandler | ||
|
||
|
||
class SyncDependency: | ||
"""Describe an async dependency""" | ||
|
||
def on_after_commit(self) -> None: | ||
"""Method called when the unit of work transaction is has been commited.""" | ||
|
||
def on_after_rollback(self) -> None: | ||
"""Method called when the unit of work transaction is has been rolled back.""" | ||
|
||
|
||
class SyncMessageHook(Generic[TMessage, TSyncUow, P]): | ||
callback: SyncMessageHandler[TMessage, "TSyncUow", P] | ||
dependencies: Sequence[str] | ||
|
||
def __init__( | ||
self, | ||
callback: SyncMessageHandler[TMessage, "TSyncUow", P], | ||
dependencies: Sequence[str], | ||
) -> None: | ||
self.callback = callback | ||
self.dependencies = dependencies | ||
|
||
def __call__( | ||
self, | ||
msg: TMessage, | ||
uow: "TSyncUow", | ||
dependencies: Mapping[str, SyncDependency], | ||
) -> Any: | ||
deps = {k: dependencies[k] for k in self.dependencies} | ||
resp = self.callback(msg, uow, **deps) # type: ignore | ||
return resp |
Oops, something went wrong.