2

Can CQRS pattern exist and/or work without mediator pattern?

I mean there are always commands and these commands have to be dispatched somehow to the handlers that perform some logic on them.

Does it mean that CQRS pattern depend on mediator pattern in order to work?

Every example I see with CQRS uses mediator pattern, does it mean it's integral part of this pattern?

Or it can work without using mediator pattern at all?

But it doesn't make much sense then...

6

Command Query Responsibility Separation (CQRS) separates reading and writing into two different models.

A "conventional" architecture looks like this:

![Diagram of a conventional architecture (Fowler)

A CQRS architecture looks like this:

![Diagram of a CQRS Architecture (Fowler)

Notice how the CQRS diagram has two endpoints servicing the UI, not just one?

The Mediator pattern defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

enter image description here

The Mediator Pattern ensures that components don't call each other explicitly, but instead do so through calls to a mediator. In the following example, the Mediator registers all Components and then calls their SetState methods.

public interface IComponent
{
    void SetState(object state);
}

public class Component1 : IComponent
{
    public void SetState(object state)
    {
        throw new NotImplementedException();
    }
}

public class Component2 : IComponent
{
    public void SetState(object state)
    {
        throw new NotImplementedException();
    }
}

// Mediates the common tasks
public class Mediator
{
    public IComponent Component1 { get; set; }
    public IComponent Component2 { get; set; }

    public void ChangeState(object state)
    {
        this.Component1.SetState(state);
        this.Component2.SetState(state);
    }
}

Mediators lend themselves naturally to the Observer Pattern and the Event Aggregator Pattern, since events are a natural way to register components with a mediator:

enter image description here

Notice how similar this diagram of the Observer Pattern is with the Mediator pattern diagram above?

So what does the Mediator Pattern have to do with CQRS?

Not much, really.

Well, other than the fact that you can use a Mediator with CQRS, and an Observer Pattern with the mediator, and probably a half-dozen other useful software patterns with both.

Further Reading
Command Query Responsibility Segregation by Martin Fowler
CQRS Example in C# at MSDN
Command query responsibility segregation on Wikipedia
Observer Pattern on Sourcemaking.com
Event Aggregator (Fowler)

  • I think the diagram is wrong because command model doesn't update the database and it doesn't contain logic. It's the handler/observer that subscribes to that command... – Konrad May 21 '18 at 17:10
  • I think command is more like a DTO – Konrad May 21 '18 at 17:11
  • Mediator pattern probably has nothing to do with CQRS but looking from the other side, CQRS seems to have much to do with mediator pattern – Konrad May 21 '18 at 17:12
  • Can the mediator be at the same time observer? For example github.com/jbogard/MediatR/blob/master/src/MediatR/Mediator.cs – Konrad May 21 '18 at 21:32
  • How would you classify this? – Konrad May 21 '18 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.