diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 762ca3dc..c35d8afa 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -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 `_. - Add extra typing test for provided instance of ``DependenciesContainer`` provider. 4.4.1 diff --git a/examples/miniapps/commands-and-handlers/README.rst b/examples/miniapps/commands-and-handlers/README.rst new file mode 100644 index 00000000..89c23713 --- /dev/null +++ b/examples/miniapps/commands-and-handlers/README.rst @@ -0,0 +1,14 @@ +Commands and Handlers Example +============================= + +This mixed example from issue `#327 `_. +It demonstrates ``Dict`` provider with non-string keys and ``.provided`` attribute. + +Run +--- + +To run the application do: + +.. code-block:: bash + + python -m application diff --git a/examples/miniapps/commands-and-handlers/application/__init__.py b/examples/miniapps/commands-and-handlers/application/__init__.py new file mode 100644 index 00000000..1c744ca5 --- /dev/null +++ b/examples/miniapps/commands-and-handlers/application/__init__.py @@ -0,0 +1 @@ +"""Top-level package.""" diff --git a/examples/miniapps/commands-and-handlers/application/__main__.py b/examples/miniapps/commands-and-handlers/application/__main__.py new file mode 100644 index 00000000..ee7c6de3 --- /dev/null +++ b/examples/miniapps/commands-and-handlers/application/__main__.py @@ -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) diff --git a/examples/miniapps/commands-and-handlers/application/commands.py b/examples/miniapps/commands-and-handlers/application/commands.py new file mode 100644 index 00000000..b5510b2a --- /dev/null +++ b/examples/miniapps/commands-and-handlers/application/commands.py @@ -0,0 +1,12 @@ +"""Commands module.""" + +class Command: + ... + + +class SaveRating(Command): + ... + + +class DoSomethingElse(Command): + ... diff --git a/examples/miniapps/commands-and-handlers/application/containers.py b/examples/miniapps/commands-and-handlers/application/containers.py new file mode 100644 index 00000000..61db0b01 --- /dev/null +++ b/examples/miniapps/commands-and-handlers/application/containers.py @@ -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, + }), + ) diff --git a/examples/miniapps/commands-and-handlers/application/handler.py b/examples/miniapps/commands-and-handlers/application/handler.py new file mode 100644 index 00000000..0d940801 --- /dev/null +++ b/examples/miniapps/commands-and-handlers/application/handler.py @@ -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') diff --git a/examples/miniapps/commands-and-handlers/application/messagebus.py b/examples/miniapps/commands-and-handlers/application/messagebus.py new file mode 100644 index 00000000..3144bf4b --- /dev/null +++ b/examples/miniapps/commands-and-handlers/application/messagebus.py @@ -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]() diff --git a/examples/miniapps/commands-and-handlers/application/repositories.py b/examples/miniapps/commands-and-handlers/application/repositories.py new file mode 100644 index 00000000..f2aaff49 --- /dev/null +++ b/examples/miniapps/commands-and-handlers/application/repositories.py @@ -0,0 +1,5 @@ +"""Repositories module.""" + + +class RatingRepository: + ...