First approach with CQRS

I’ve started to use CQRS at work last week.

On my project, this used to be the architecture layering: an MVC app, with some logic on the Controllers that call n services that get the data from the repositories, bring the model back to the controller and render the page. Job done.

Number of classes needed to get some data from the DB to the UI:

1 controller + 1 test class

1 query class

1 service + 1 test class

1 repository + 1 test class

1 response data model

 

8 files

image2

We’ve recently started to investigate CQRS, and we are now having 2 different scenarios: query and command.

The most complex scenario is when The UI call a controller action to do something and get some data back. in this case I need to create:

1 controller + 1 test class

1 query object

1 service to wrap the business logic + 1 test class

1 command handler + 1 test class

1 command

1 repository to read from the db + 1 test class

1 repository to write to the db + 1 test class

1 response data model

13 classes

image1

 

Basically to solve the most common problem (execute a command and get some data back to the UI) I now need 13 classes rather than the usual 8 I was using before.

It looks like a lot more work to do, let’s hope the Responsibility Segregation and the ease of testing each different class and piece of code will be good in the long term.

 

I got a code example for CQRS from Mark Nijhof:
MarkNijhof/Fohjin – GitHub http://bit.ly/v2c9Ts

Few more articles I recommend to read:

Martin Fowler: CQRS http://bit.ly/sFPqcR
CQRS, Task Based UIs, Event Sourcing agh! | Greg Young http://bit.ly/uUkgr7
Elegant
Code » CQRS à la Greg Young http://bit.ly/uZUhsE

Advertisements

2 thoughts on “First approach with CQRS

  1. Thanks for sharing this. Not really sure why we need to have separate repositories for read and write? I would also recommend to have a look at the implementation of these concepts in SharpArch 2.0. Do you have any sample application that you could share with this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s