CQRS – First step – Split to commands and queries

This post series is driven by my lightning talk about how to introduce CQRS to your project. I thought that would be good to explain this topic further for people who won’t be attending my presentation.ople who won’t be attending my presentation.

Recent state of your app

Let’s assume that you work on an e-commerce platform. You got plenty of controllers, services and repositories in your project. Every controller use services, services use other services, multiple repositories, validators and helpers. Dependency level increase time and effort to find correlations between components and context in your application.

You think that system that will advantage from introducing CQRS but you find it too overwhelming and hard to implement in the current situation. On the other hand, you feel that multiple parts of your codebase are painfully difficult to understand and you want to do something with that. You would like to implement thispattern step by step, without hard breakings in your current code.

You find first weak spot – product management. Your products contain dynamic fields, with own validation for every defined field. Moreover fields are bound to categories so we cannot add a field value to a product that is not corresponding to a product category. At the beginning of the project you followed this article about creating and validating a dynamic model which resulted in such product structure.

These models are heavily used by ProductsService – mainly consumer of logic from ProductsController. In this place, you want to start your focus. Below there is a simplified view of this service, with only 2 actions listed:

As you can see, only these 2 methods have already created a case for injecting 4 different dependencies. 100 lines of code for merely 2 requests, when this service needs to handle a lot of them. With more internal logic your service will start looking similar to nopCommerce‘s one – ProductService.cs. Furthermore, every change or enhancement in a request to the ProductController has to be reflected with a modifying behaviour of this service – it will lead to increasing of complexity, impeding writing and maintaining unit tests and prevent this service from future refactors.

Your ProductService demands some action.

Command and queries

CQRS is a simple pattern – two objects for command/queries where once there was one.

This quote is from Jimmy Bogardpost which teaches you how easy is first step in your application to introduce first elements of CQRS.

Jimmy created awesome library called MediatR which provides an in-process messaging component to your system. It extremely simplifies process of sending command and queries and responding to them – you don’t need to created your own class structure, everything is given out-of-the-box.

In previous code you structure GetProductsQuery and ChangeProductCategoryCommand, both implement IRequest interface.

GetProductsQuery.cs

1

2

3

4

5

6

7

8

9

10

publicclassGetProductsQuery:IRequest<IEnumerable<Product>>

{

publicintPage{get;set;}

publicintTake{get;set;}

publicint?CategoryId{get;set;}

publicDictionary<int,object>FieldValues{get;set;}

}

ChangeProductCategoryCommand.cs

1

2

3

4

5

6

7

8

publicclassChangeProductFieldValueCommand:IRequest

{

publicintProductId{get;set;}

publicintFieldId{get;set;}

publicobjectFieldValue{get;set;}

}

As parameter type you need to define what is the type of the returned element, after handling request – as you can see command is returning nothing but the query is returning a list.

Then you define handlers of these requests – they need to implement IRequestHandler interface, with query / command as parameter type.

And this is it – you went your first step in CQRS journey. Command and query are defined in different files which can be treated separately with different approach about database querying, parameters validation and internal logic.

The advantage from this step:

Moving from typical layered pattern to Command/Query one, without further actions, has several advantages:

Concern on Single Responsibility Principle – one class which handles a request.

Clearly defined target and dependencies.

Avoidance of mixing code which modifies data and returns it.

Deeper view of your system – finding a correlation between handlers, not layers.

Less effort in maintenance and more detailed refactoring.

Ease of writing unit tests.

In our case, such change gave us a better understanding of our system and possibility to move toward more complicated solutions.

The moment of denying

Wait, but you have seen in the internet piles of articles and videos how people running their CQRS app with event sourcing, multiple databases, message queues etc. Complicated diagrams, layers, components, arrors from one way to another…

You don’t need it. Not at the beginning.

In the simplest form, CQRS model don’t need to be treated as a different database model but as different in and out objects. Splitting model to the separate storages gives a lot of value, but maintenance of a distributed system with eventual consistency model is definitely tougher that a monolit’s one with a single processed request.

Just remember that nobody introduced this pattern to an existing application, with so many components, in one day. Focus on a single thing, refactor your logic into separated command and queries, and then go further for greater good. Such splitting to simple handlers, won’t break your application and you will get a better insight of how your system is working. Then you can take next step.

Related Posts

About me

Welcome on my blog!

My name is Radek Maziarka and I solve problems ;)

On the daily basis, I work in Objectivity as Technology Development Manager. My duties, both in work and outside, are connected to broadly understood programming / architecture / leadership / management.