Click here to Skip to main content
15,884,628 members
Articles / Hosted Services / Azure

CQRS with Decoupled Messaging - Part I

Rate me:
Please Sign up or sign in to vote.
4.56/5 (8 votes)
4 Mar 2016CPOL3 min read 40.7K   24   10
First article in a series of articles that show practical application of CQRS architecture with emphasis on decoupling messaging as infrastructure component


DDD and CQRS have a steep learning curve. There are many articles and code samples that explain the basics of CQRS architecture in a simplified manner. However, when it comes to applying CQRS to a real world application (for example: with using Enterprise Service bus like Azure Servicebus, RabbitMQ or MSMQ), many implementations couple these messaging concerns with core domain. This makes applying CQRS to the real world application difficult.

This series of articles show "how" to achieve architecture with decoupled infrastructure concerns so that the end solution is simple to follow and maintain.


  1. Knowledge of DDD
  2. Basic understanding of CQRS architecture
  3. Azure Web and Worker Roles
  4. Azure service bus


DDD emphasizes the need to keep the domain model free of infrastructure and application layer concerns. As depicted in the figure below:

Image 1

As shown above, the dependencies point inward towards Domain layer. No Layer depends on infrastructure's concrete implementation. Main can be used to bootstrap the dependencies which will be called upon by the Presentation layer.

In DDD, communication across aggregates is done via messaging. In CQRS, messaging is required for sending commands and publishing Events. Thus, messaging becomes a key Infrastructure component when implementing such an architecture.

However, messaging is still an infrastructure component and thus should be abstracted out from the domain. We will create an interface IServicebus for the same and see how different Servicebus frameworks like Masstransit, nServicebus (with different transports like msmq, Azure servicebus) can be used as concrete implementation of the same in the infrastructure layer.

public interface IServiceBus : IDisposable
    void Publish(IEvent eventMessage);
    void Send(ICommand commandMessage);

Thus, all of the layers (except Infrastructure) will work with IServiceBus interface and will not have direct code dependency on concrete implementation in Infrastructure messaging component. This makes swapping the third party libraries easy with some other implementation and also it does not get into our way when implementing our business logic.

What Will Be Covered in This Series?

Here is a brief introduction to articles in this series.

  1. Introduction
    • Current article explaining the principles being followed.
  2. Need for Enterprise Servicebus Frameworks and Decoupled messaging with their samples
    • We will look at samples from MassTransit and nServicebus documentation and will decouple publish subscribe from those samples.
  3. Inventory Manager - CQRS application (with decoupled messaging) - Commands
    • Inventory Manager application will be introduced. It is implementation of Greg Young's Simple CQRS Example, however making it usable for real world. Currently Inventory Manager application implements only first usecase of creating an inventory item.
    • The post will focus on sending command following CQRS architecture in Inventory Manager application.
  4. Inventory Manager - CQRS application (with decoupled messaging) - Aggregate and Event Sourcing
    • Explaining the workings of Aggregate and Event Sourcing in Inventory Manager application
    • Focuses on how to ensure publishing of events upon saving of aggregate (without Distributed Transaction)
  5. Inventory Manager - CQRS application (with decoupled messaging) - ReadSide
    • Thin Read layer in Inventory Manager and points of interest about implementing it


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

SuggestionDDD Pin
Member 126022362-Sep-16 2:47
Member 126022362-Sep-16 2:47 
PraiseWell done Pin
Doug Hieber17-Jun-16 3:48
Doug Hieber17-Jun-16 3:48 
PraiseExcellent Pin
Rupesh Kumar Tiwari5-Mar-16 4:56
Rupesh Kumar Tiwari5-Mar-16 4:56 
GeneralMy vote of 5 Pin
neel_554-Mar-16 12:09
neel_554-Mar-16 12:09 
QuestionNot an article and ... Pin
CHill604-Mar-16 2:22
mveCHill604-Mar-16 2:22 
AnswerRe: Not an article and ... Pin
Rishabh S Ajmera4-Mar-16 15:23
Rishabh S Ajmera4-Mar-16 15:23 
GeneralRe: Not an article and ... Pin
CHill605-Mar-16 0:33
mveCHill605-Mar-16 0:33 
GeneralRe: Not an article and ... Pin
Nelek5-Mar-16 3:20
protectorNelek5-Mar-16 3:20 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun2-Mar-16 20:20
Humayun Kabir Mamun2-Mar-16 20:20 
SuggestionNot an article Pin
Akhil Mittal2-Mar-16 18:03
professionalAkhil Mittal2-Mar-16 18:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.