When the application of modern company applications in size and complexity, maintains a clean modular architecture to be critical. Traditional layered designs often fail when performance, scalability, and separation of obvious concerns are needed.
That’s where Apply CQRS and Mediatr on Asp.net Core Can change your software architecture – offer a very easy to test, be combined loosely, and can be maintained.
On this blog, we will guide you through the application process CQRS pattern with mediatr In the ASP.NET core project, explore the principles of real world design, and share practical tips that are tailored to the team that builds the company’s class applications.
๐ What is CQRS on Asp.net core?
CQRS (Segregation of the Responsibility of the Command of the Command) is a design pattern that separates the reading operation from writing operations. In large applications, this segregation can cause an increase in performance, scalability, and maintenance.
- Rule – Changing the state of the application (for example, createdero, updatecustomer).
๐ Benefits of CQRS on Asp.net Core:
- The scale of reading and writing operations independently.
- Business logic is easier to reason.
- The reading model can be optimized separately from the written model.
๐งฉ How Mediatr Complete CQRS
Mediatr is a popular .net library that implements Mediator design patternHelps you separate the sender of the request from the handler. Instead of asking for services directly, you send order And Kueri through immediately.
CSHARP
Copyedit
// traditional service calls
_orderservice.createorder (CMD);
// with mediatr
waiting for _mediator.send (cmd);
Using mediatr on asp.net core supports a Separation that is clean from concernsEliminating strict clutch, and promoting better testability – key advantages for company software systems.
๐ CQRS + MediaTr project structure in Asp.net Core
Say we are building Order management system. Modular folder structure may look like this:
price reduction
Copyedit
/Feature
/Order
– Createdercommand.cs
– Createorderhandler.cs
– Getorderbyidquery.cs
– Getorderbyidhandler.cs
๐งฐ Piles of Technology:
๐ Prepare the core of asp.net with mediatr and fluentvalidation
First, install the required nuget package:
party
Copyedit
Dotnet add mediatr.extensions.microsoft.dependencyininjection package
Dotnet Add FluentValidation Package
Then, configure the program.cs:
CSHARP
Copyedit
builder.services.addmediatr (CFG =>
Cfg.registerser servicesfromassembly (Typeof (program). Assembly));
builder.services.addvalidatorsfromassembly (Typeof (program). Assembly);
This setting allows your commands and queries to be transferred and validated without confiscating your controller.
๐งพ Apply the command: createder
Createordercommand.cs
CSHARP
Copyedit
Createdercommand (Customername String, Register Public Records
Createorderhandler.cs
CSHARP
Copyedit
Public Class Creenatedler: Irequesthandler
{
Private Readonly AppdBcontext _DB;
Public CreatedDandler (Appdbcontext DB)
{
_DB = DB;
}
ASYNC Public Duties
{
is an order = new order
{
Id = guid.newguid (),
Customername = request.customername,
Item = string.jin (“,”, Request.items),
Createtat = datetime.utcnow
};
_db.orders.add (order);
waiting for _db.savechangesasync (cancellatingontekn);
return order.id;
}
}
Validation with fluentvalidation
CSHARP
Copyedit
Public Class Createdagalidator: Abstractvalidator
{
Public createdatoridator ()
{
Rulefor (x => x.customename). Notempty ();
Rulefor (x => x.items). Notempty (). Withmessage (“Order must have at least one item.”);
}
}
๐ Applying the queue: Getorderbyid
Getorderbyidquery.cs
CSHARP
Copyedit
Public records of Getorderbyidquery (Guid Orderid): Irequest
Getorderbyidhandler.cs
CSHARP
Copyedit
Public Class Getorderbyidhandler: Irequesthandler
{
Private Readonly AppdBcontext _DB;
Public Getorderbyidhandler (Appdbcontext DB)
{
_DB = DB;
}
ASYNC Public Duties
{
var order = waiting _db.orders.findasync (request.orderid);
if (order == null) return null;
Restore new orders
{
Id = order.id,
Customername = order.customername,
Item = order.items.split (‘,’). Tolist (),
Createtat = order.createdat
};
}
}
๐ Using CQRS and Mediatr on Asp.Net Core Controllers
CSHARP
Copyedit
[ApiController]
[Route(โapi/[controller]”)]
Public class orders: controllerbase
{
Private Readonly Impediator _mediator;
Public Public (Mediator Emediator)
{
_mediator = mediator;
}
[HttpPost]
ASYNC Public Duties
{
var id = waiting for _mediator.send (cmd);
Return Creatacaction (Nameof (Get), New {id}, null);
}
[HttpGet(โ{id}โ)]
ASYNC Public Duties
{
var order = waiting for _mediator.send (new Getorderbyidquery (ID));
Return order == NULL? Notfound (): OK (order);
}
}
๐ก Real world benefits of CQRS and Mediatr in the company system
โ Main Benefits:
- Separation of concerns – Every handle focuses on one responsibility.
- Testability – Easier to write unit tests for individual handling.
- Scalability – Read the operation can be independently scale from writing.
- Pipe behavior -Activating cross features such as logging and transactions.
โ ๏ธ Public traps that should be avoided:
- Too often using simple applications – CQRS may not be necessary for Basic CRUD operations.
- Non -aligned validation – Use a centered validator with fluentvalidated.
- Inconsistent limit – Know when and where to separate commands and questions.
๐ When should you use CQR and Mediatr on Asp.Net Core?
Ideal use cases:
- Applications with complex business rules.
- Scenario where reading and writing have very different performance needs.
- The team looking for separated architecture that supports scalability and DDD.
Avoid when:
- Your application is an internal tool or a simple admin dashboard.
- Strict project schedule and impossible for architectural overhead.
๐ Conclusion: Applying CQRS and Mediatr on Asp.net Core
For the core application asp.net the company class, Apply CQRS and Mediatr Bring the structure, scalability, and testing to your software architecture. By separating clear responsibilities, you empower your team to develop a stronger, nurtured, and ready solution in the future.
Whether you start fresh or modernize the inheritance system, this pattern is worth considering because of its ability to simplify complex logic and increase long -term agility.
Additional resources:
Game Center
Game News
Review Film
Berita Olahraga
Lowongan Kerja
Berita Terkini
Berita Terbaru
Berita Teknologi
Seputar Teknologi
Berita Politik
Resep Masakan
Pendidikan
Berita Terkini
Berita Terkini
Berita Terkini
review anime
Gaming Center
Originally posted 2025-08-03 14:49:01.