MediatR and CQRS Pattern | Software Architecture 101

software architecture tech background image

The goal of this blog post is simple..

First, we talk about CQRS software pattern and its use cases… why it is so damn important to have a decoupled application achieved by the CQRS Pattern.Then we’ll dive into what MediatR is and what it can do…

CQRS Pattern

This image below explains about two models within the Application layer, which is the Query and Command model.

The Front-End UI communicates with these models to create, update, safe and receive data from the Database.


Why CQRS?

Single Responsibility 

Single Responsibility is a computer programming principle. It states that every module or class should have responsibility for a single part of the functionality provided by the software. Therefore, all entities are each a Single Responsibility.

Benefits of Single Responsibility:

  1.  It makes it easier to edit and add functionality to a class/model that has a single responsibility 
  2. Will achieve loosely coupled application
  3. Independent of framework
  4. Code is more readable and understandable

This is how I segregate the
Command and Query Responsibility Segregation pattern

[ApplicationName].Core.Application

an image showing customers folder in visual studio
an image showing customers folder in visual studio
an image showcasing folders in visual studio
Folders within the “Customers” folder

As you can see above, I have a Customers folder and inside that folder contains the commands, models, queries, and validators.

For example in my case, I have a “CreateCustomerCommand.cs'” and “CreateCustomerCommandHandler” in the Commands folder. I also have “get” classes which are structured similarly in naming convention; “GetAllCustomersQuery.cs” and “GetAllCustomersQueryHandler.cs“.

The Commands are for Creating, Updating and Deleting objects/entities. The queries are for searching and getting data back out from the database.

An entity, is basically a “thing“. It can be anything . An entity could be a customer, a book or a coffee.

Entity definition for the haters

entity definition from google
entity definition sourced from google

MediatR – Jimmy Bogard

So this guy Jimmy did a great job on implementing a library for the function of CQRS pattern. Benefits include:

  • Simple mediator implementation in .NET
  • In-process messaging with no dependencies.
  • Supports request/response, commands, queries, notifications and events, synchronous and async with intelligent dispatching via C# generic variance.
  • Any block can opt into these alignments. The embed block has them also, and is responsive out of the box:
Definitely watch this video, it is great!!! Jimmy and the team does a great job explaining CQRS!

Using MediatR in ASP.Net Core was slightly tricky, not many out there with the correct implementation. It actually is very simple, but it took me some time to figure it all out. (I’m not a newbie in the .NET Framework)

Here is some code for in the StartUp.cs class you can use to register the MediatR service:

// ADD MediatR
services.AddMediatR();
services.AddMediatR(typeof(CreateCustomerCommand).Assembly, typeof(CreateCustomerCommandHandler).Assembly);

See what I did there uptop, I registered the Command and the Command Handler

Don’t forget to do this!

The controllers in the MVC model will reference these commands, queries. 

The beauty is that MediatR handles the requests in the pipeline.

A “CreateCustomerCommand.cs” implementation:

namespace QAEngine.Application.Customers.Command
{
  public class CreateCustomerCommand : IRequest
  {
    public string Id { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public DateTime AccountCreated { get; set; }
    public DateTime DateOfBirth { get; set; }
  }
}

A “CreateCustomerCommandHandler.cs” implementation:

namespace QAEngine.Application.Customers.Command
{
 public class CreateCustomerCommandHandler : IRequestHandler
 {
   private readonly QAEngineDbContext _context;  
   public CreateCustomerCommandHandler(QAEngineDbContext context)
   {
     _context = context;
   }

    public async Task<CustomerModel> Handle(CreateCustomerCommand 
    request, CancellationToken cancellationToken)
    {
        var entity = new Customer
        {
            CustomerId = request.Id,
            Title = request.Title,
            FirstName = request.FirstName,
            LastName = request.LastName,
            Address = request.Address,
            City = request.City,
            Region = request.Region,
            PostalCode = request.PostalCode,
            Country = request.Country,
            Phone = request.Phone,
            AccountCreated = request.AccountCreated,
            DateOfBirth = request.DateOfBirth.Date
        };

        _context.Customers.Add(entity);
        await _context.SaveChangesAsync(cancellationToken);
        return CustomerModel.Create(entity);
    }
  }
}

Finally, Here is my Customer controller, where I utilise these beautiful implementations <3

    [HttpGet()]
    public Task<List<CustomerListModel>> ViewCustomers()
    {
        return Mediator.Send(new GetCustomerListQuery());
    }


    [HttpGet("{id}")]
    public async Task<IActionResult> FindACustomer(string id)
    {
        return Ok(await Mediator.Send(new GetCustomerModelQuery { Id = id }));
    }


    [Authorize]
    public IActionResult NewCustomer()
    {

        return View();
    }

    [Authorize]
    [HttpPost]
    //noteToSelf = FromForm means from the bloody form not [FromBody]
    public async Task<IActionResult> NewCustomer([FromForm]CreateCustomerCommand command)
    {


        return Ok(await Mediator.Send(command));

    }

    [HttpPut("{id}")]
    public async Task<IActionResult> UpdateCustomerDetails(string id, [FromBody]UpdateCustomerCommand command)
    {
        if (command == null || command.Id != id)
        {
            return BadRequest();
        }


        return Ok(await Mediator.Send(command));
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteCustomer(string id)
    {
        await Mediator.Send(new DeleteCustomerCommand { Id = id });
        return NoContent();
    }

Code is Poetry

G

?

Read my journey: https://www.bilaalsblog.com/pilot-a-software-developers-journey-and-the-amount-of-coffees-consumed/

Microsoft Technology Stack Software Development C# ASP .NET Framework
MediatR                                                https://github.com/jbogard/MediatR 

22 thoughts on “MediatR and CQRS Pattern | Software Architecture 101”

  1. Heya terrific blog! Does running a blog such as this require a large amount of work? I have no understanding of computer programming however I had been hoping to start my own blog in the near future. Anyways, if you have any ideas or techniques for new blog owners please share. I know this is off subject nevertheless I simply needed to ask. Kudos!

  2. Howdy this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors
    or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get
    guidance from someone with experience. Any help would be enormously appreciated!

    1. Hello there,

      Yes blogs do use WYSIWYG editors.. WordPress is a great platform! and it always has been!

      What are you going to blog about?

  3. Hello,
    Very nice blog. I am trying to explore CQRS/MediatR approach for some time now.
    I always lack examples of some more complex scenario with CQRS/MediatR. I work on a relatively big enterprise project in healthcare that is ready for change. Do you maybe have experience with applications implemented in this pattern that have high and heavy traffic ? I am interested in cons of using in-process messages. Also healthcare data is very interconnected and sometimes I am not sure about separation.
    Thanks! Cheers!

    1. Hi Tarik

      Separation is always best. When you mention healthcare i think of micro services. These are services which are totally loosely coupled to one another. All micro-services contain their own endpoint in the API design as well as a direct route to the database. A micro-services architecture would be best suited for you since this architecture will provide both speed and reliability. Deploying micro-services to the cloud will further enhance the design and architecture.

  4. When s᧐meone writes an ρiece of writing he/she keeps the idea of a useг in his/her mіnd that
    how a սser can սnderstand it. So that’s why thіs piece of writing is perfect.
    Thanks!

  5. Heya i’m for the first time here. I found this
    board and I find It really useful & it helped me out much.
    I hope to give something back and aid others like you aided me.

  6. Its such as you read my mind! You appear to understand a lot about this, such as you wrote the e-book in it or something. I believe that you could do with some percent to pressure the message house a little bit, however other than that, this is magnificent blog. A great read. I’ll definitely be back.

  7. When someone writes an post he/she maintains the plan of a user in his/her mind that how a user
    can be aware of it. Therefore that’s why this post is outstdanding.
    Thanks!

  8. Hello. I have checked your bilaalsblog.com and i see you’ve got some duplicate content so probably it is the
    reason that you don’t rank high in google. But you can fix this issue fast.
    There is a tool that creates articles like human, just search in google:
    miftolo’s tools

  9. Pretty section of content. I just stumbled upon your website and in accession capital to assert that I
    acquire actually enjoyed account your blog posts. Anyway
    I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

  10. I as well as my buddies came checking the nice secrets located on your web blog and then the sudden I got an awful feeling I never thanked the site owner for them. The women became as a consequence thrilled to read through them and have now absolutely been taking pleasure in those things. Many thanks for simply being really accommodating and then for figuring out certain brilliant guides millions of individuals are really needing to understand about. My personal honest regret for not saying thanks to you sooner.

Leave a Reply

Your email address will not be published. Required fields are marked *