Giter Site home page Giter Site logo

command bus about gnugat.github.io HOT 1 CLOSED

gnugat avatar gnugat commented on August 22, 2024
command bus

from gnugat.github.io.

Comments (1)

gnugat avatar gnugat commented on August 22, 2024

Hi @FlorianCcj, I'm glad I can help!

If you consider the following interfaces:

<?php

interface CommandHandler
{
    public function supports($command): bool
    public function handle($command);
}

interface CommandBus
{
    public function register(CommandHandler $commandHandler);
    public function handle($command);
}

You can have a "base" CommandBus implementation that registers CommandHandlers, and then when given a Command will try to find a Handler that supports it:

<?php

class RegisteringCommandBus implements CommandBus
{
    private $commandHandlers = [];

    public function add(CommandHandler $commandHandler)
    {
        $this->commandHandlers[] = $commandHandler;
    }

    public function handle($command)
    {
        foreach ($this->commandHandlers as $commandHandler) {
            if ($commandHandler->supports($command)) {
                return $commandHandler->handle($command);
            }
        }
        throw new NotSupportedException('The given Command is not supported by any registered CommandHandlers.');
    }
}

For most use cases, this is enough, but sometimes you want to always execute some code before or after all or some CommandHandlers, which is where the "Middleware" pattern is used: you create an implementation that does that extra task, and you inject in it the "base" implementation so you can call it before or after that task.

For example here's a CommandBus implementation that always log a message after any command handler have been executed:

<?php

class LogCommandBus implements CommandBus
{
    private $commandBus;
    private $logger;

    public function __construct(
        CommandBus $commandBus,
        Logger $logger
    ) {
        $this->commandBus = $commandBus;
        $this->logger = $logger;
    }

    public function handle($command)
    {
        $value = $this->commandBus->handle($command);
        $this->logger->log("C'est pas faux");
 
        return $value;
    }
}

In most of my use cases, I have no use for Middleware, so I usually skip the CommandBus entitrely but still stick to the Command / CommandHandler. You can read more about this here: http://gnugat.github.io/2017/09/20/pragmaticlean-command-bus.html

I hope this is useful, let me know if you have more questions.

from gnugat.github.io.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.