CQRS – Using Queries or Read Services Only?

cqrsrepository-pattern

Discussing about a CQRS implementation with my team we don’t agree on a point concerning the Query side.

On the Commad side we have actual command clases; a CommandDispatcher that schedules commands, defines a Unit of Work scope and dispatches commands to handlers; handlers that contain the business logic and do CRUD operations using repositories.

For the Query side the team is split on two:

  1. One part of the team says that a simple read service layer is
    enough, services that access directly the data source (no
    repositories) and can be called directly from callers (no query
    classes nor a query dispatcher).
  2. The other part of the team says that we should build the Query
    side symmetrically to the Command side, having query classes, a
    query dispatcher and fetch data from repositories.

We don’t need any synchronization nor scheduling system for queries (query dispatcher) and query handlers must be mostly classes that return data fetched from repositories. So we would add potentially unnecessary extra layers only for the sake of symmetry.

Any thought?

Best Answer

Disclaimer It may be personal opinion.

There exists another design principle: KISS -Keep It Simple...

Simple Repository can make some advantage as can simplify the code of your services, but going deeper can not be certainly necessary.

You have to think out what are your business values of making over-engineered query engine.

  • Do you expect that you will need to pass queries between subsystems? If so then some dispatcher can be helpful (I would name it router then anyway).

  • Do you expect that you will need to add new query parameters frequently? If so, query classes will be certainly needed.

Related Topic