Mediator pattern impl
Project description
Description
Mediator pattern impl
Docs
Available
- Dispatchers:
- Command
- Query
- Observers:
- Event
- Entities:
- Command
- Event
- Query
- Middlewares:
- Middleware
Usecases
Command/Event/Query
from dataclasses import dataclass
from mediator.dispatchers import CommandDispatcherImpl
from mediator.entities import Command
from mediator.interfaces.handlers import ICommandHandler
@dataclass
class IntCommand(Command[int]):
answer: int
class IntCommandHandler(ICommandHandler[IntCommand, int]):
async def __call__(self, request: IntCommand) -> int:
return request.answer
async def main():
c = CommandDispatcherImpl()
c.register(IntCommand, IntCommandHandler())
await c.handle(IntCommand(1))
Middleware
from dataclasses import dataclass
from mediator.dispatchers import CommandDispatcherImpl
from mediator.entities import Command, Request
from mediator.interfaces.handlers import ICommandHandler
from mediator.middlewares.base import Middleware
class SimpleMiddleware(Middleware):
async def __call__(self, request: Request):
return await self.call_next(request)
@dataclass
class IntCommand(Command[int]):
answer: int
class IntCommandHandler(ICommandHandler[IntCommand, int]):
async def __call__(self, request: IntCommand) -> int:
return request.answer
async def main():
c = CommandDispatcherImpl(middlewares=[SimpleMiddleware])
c.register(IntCommand, IntCommandHandler())
await c.handle(IntCommand(1))
Tests
pytest tests
Additional
Inspired by didator
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
meator-0.1.1a4.tar.gz
(6.0 kB
view hashes)
Built Distribution
meator-0.1.1a4-py3-none-any.whl
(12.3 kB
view hashes)