Sunday, December 18, 2022

CQRS and MediatR

   In this article, we are going to discuss the working of CQRS and MediatR patterns and step-by-step implementation using .NET Core 6 Web API.

Agenda

  • Introduction of CQRS Pattern
  • When to use CQRS
  • MediatR

Introduction of CQRS Pattern

  • CQRS stands for Command and Query Responsibility Segregation and uses to separate read(queries) and write(commands).
  • In that, queries perform read operation, and command perform writes operation like create, update, delete, and return data.
  • As we know, in our application we mostly use a single data model to read and write data, which will work fine and perform CRUD operations easily. But, when the application becomes a vast in that case, our queries return different types of data as an object so that become hard to manage with different DTO objects. Also, the same model is used to perform a write operation. As a result, the model becomes complex.
  • Also, when we use the same model for both reads and write operations the security is also hard to manage when the application is large and the entity might expose data in the wrong context due to the workload on the same model.
  • CQRS helps to decouple operations and make the application more scalable and flexible on large scale.

When to use CQRS

  • We can use Command Query Responsibility Segregation when the application is huge and access the same data in parallel. CQRS helps reduce merge conflicts while performing multiple operations with data.
  • In DDD terminology, if the domain data model is complex and needs to perform many operations on priority like validations and executing some business logic so in that case, we need the consistency that we will by using CQRS.

MediatR

  • MediatR pattern helps to reduce direct dependency between multiple objects and make them collaborative through MediatR.
  • In .NET Core MediatR provides classes that help to communicate with multiple objects efficiently in a loosely coupled manner.

Tuesday, March 1, 2011

Convert value in different format

Sub Main()
      Dim d1, d2, d3 As Double
      Dim i1, i2, i3 As Byte
      Dim dec1 As Decimal
      Dim int1 As Integer
      dec1 = 15.27
      int1 = 32000
      d1 = 23.4567
      d2 = 14.01
      d3 = 0.003456
      i1 = 254
      i2 = 37
      i3 = 64
      Console.WriteLine("F6 " + d1.ToString("F6")) ' 23.456700 six digit right of decimal point
      Console.WriteLine("F4 " + d2.ToString("F4")) ' 14.0100  four digits right of dec. point
      Console.WriteLine("C format " + dec1.ToString("C")) ' $15.27
      Console.WriteLine("D4 " + int1.ToString("D7")) '  0032000
      Console.WriteLine("D8 " + i1.ToString("D8")) ' 00000254 leading zeros 8 digits
      Console.WriteLine("X4 " + i2.ToString("X4")) ' four digits hex w/leading zeros
      Console.WriteLine("X8 " + i3.ToString("X8")) ' eight digits hex with leading zeros
 End Sub