mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-06 06:30:51 +03:00
Add commands and handlers example
This commit is contained in:
parent
fbaf35244c
commit
cd2c5697c3
|
@ -11,6 +11,8 @@ Develop
|
|||
-------
|
||||
- Add support of non-string keys for ``Dict`` provider.
|
||||
- Add simple ``FastAPI`` example.
|
||||
- Add ``Commands and Handlers`` example from
|
||||
issue `#327 <https://github.com/ets-labs/python-dependency-injector/issues/327>`_.
|
||||
- Add extra typing test for provided instance of ``DependenciesContainer`` provider.
|
||||
|
||||
4.4.1
|
||||
|
|
14
examples/miniapps/commands-and-handlers/README.rst
Normal file
14
examples/miniapps/commands-and-handlers/README.rst
Normal file
|
@ -0,0 +1,14 @@
|
|||
Commands and Handlers Example
|
||||
=============================
|
||||
|
||||
This mixed example from issue `#327 <https://github.com/ets-labs/python-dependency-injector/issues/327>`_.
|
||||
It demonstrates ``Dict`` provider with non-string keys and ``.provided`` attribute.
|
||||
|
||||
Run
|
||||
---
|
||||
|
||||
To run the application do:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
python -m application
|
|
@ -0,0 +1 @@
|
|||
"""Top-level package."""
|
|
@ -0,0 +1,12 @@
|
|||
"""Main module."""
|
||||
|
||||
from .containers import Container
|
||||
from .commands import SaveRating, DoSomethingElse
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
container = Container()
|
||||
message_bus = container.message_bus()
|
||||
|
||||
message_bus.handle(SaveRating)
|
||||
message_bus.handle(DoSomethingElse)
|
|
@ -0,0 +1,12 @@
|
|||
"""Commands module."""
|
||||
|
||||
class Command:
|
||||
...
|
||||
|
||||
|
||||
class SaveRating(Command):
|
||||
...
|
||||
|
||||
|
||||
class DoSomethingElse(Command):
|
||||
...
|
|
@ -0,0 +1,23 @@
|
|||
"""Containers module."""
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
|
||||
from . import repositories, handler, messagebus, commands
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
rating_repository = providers.Singleton(repositories.RatingRepository)
|
||||
|
||||
command_handler = providers.Singleton(
|
||||
handler.CommandHandler,
|
||||
rating_repo=rating_repository,
|
||||
)
|
||||
|
||||
message_bus = providers.Factory(
|
||||
messagebus.MessageBus,
|
||||
command_handlers=providers.Dict({
|
||||
commands.SaveRating: command_handler.provided.save_rating,
|
||||
commands.DoSomethingElse: command_handler.provided.something_else,
|
||||
}),
|
||||
)
|
|
@ -0,0 +1,14 @@
|
|||
"""Handlers module."""
|
||||
|
||||
from .repositories import RatingRepository
|
||||
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self, rating_repo: RatingRepository):
|
||||
self.rating_repo = rating_repo
|
||||
|
||||
def save_rating(self):
|
||||
print('Saving rating')
|
||||
|
||||
def something_else(self):
|
||||
print('Doing something else')
|
|
@ -0,0 +1,14 @@
|
|||
"""Message bus module."""
|
||||
|
||||
from typing import Dict, Callable, Any
|
||||
|
||||
from .commands import Command
|
||||
|
||||
|
||||
class MessageBus:
|
||||
|
||||
def __init__(self, command_handlers: Dict[str, Callable[..., Any]]):
|
||||
self.command_handlers = command_handlers
|
||||
|
||||
def handle(self, command: Command):
|
||||
self.command_handlers[command]()
|
|
@ -0,0 +1,5 @@
|
|||
"""Repositories module."""
|
||||
|
||||
|
||||
class RatingRepository:
|
||||
...
|
Loading…
Reference in New Issue
Block a user