CQRS

Command Query Responsibility Segregation is a simple concept. When you want something to happen, you will use a Command. If you need results returned, you will use a query.

An over simplified example of this would be if you need to write to a database, you would use a Command. If you need results from a database, you would use a Query.

Installation

composer require sonsofphp/cqrs

Basic Usage

Command Bus

<?php

use SonsOfPHP\Component\Cqrs\Command\CommandBus;

$commandBus = new CommandBus();
$commandBus->addHandler(CreateUser::class, $handler);

$command = new CreateUser();
$commandBus->dispatch($command);

!!! success "Symfony CQRS Bridge" Once the CQRS Symfony Bridge is installed, you can use the Command Bus that comes with that to gain access to additional features and functionality.

Query Bus

!!! success "Symfony CQRS Bridge" Once the CQRS Symfony Bridge is installed, you can use the Query Bus that comes with that to gain addition features and functionality.

Messages

Both Commands and Queries are considered to be messages. It is HIGHLY recommended to use the AbstractMessage class for both Commands and Queries. For all examples I will assume you have extended this class.

!!! note AbstractMessage treats the message as a value object. So using with will return a new instance of the class.

Additional AbstractMessage API

Message Handlers

Both Command and Query Handers are assumed to just be message handlers.

Command Handlers

Query Handlers

Symfony Bridge

The Symfony Bridge uses Symfony Components to add additional functionality to the CQRS component.

Installation

Last updated

Was this helpful?