CQRS in ASP.NET Core: Why Your CRUD App Needs a Superpower!

CRUD is Old School – Time to Level Up!

If you’ve ever built a basic ASP.NET Core application, you’ve probably followed the good old CRUD (Create, Read, Update, Delete) approach. It’s simple, it works… until it doesn’t. 😅

Imagine your app scales up, data grows, and suddenly, your once-speedy queries start dragging like an old Windows XP booting up. That’s where CQRS (Command Query Responsibility Segregation) swoops in like a superhero to save the day! 

In this post, we’ll dive deep into CQRS in ASP.NET Core, understand why it’s awesome, and even build a small example to see it in action. Buckle up!🚀

What is CQRS? 🤔

CQRS is a pattern that separates the "read" and "write" operations of an application. Instead of having a single model for both, we split them into:

  1. Command Side (Writes) 📝 – Handles operations that change data, like Create, Update, and Delete.

  2. Query Side (Reads) 👀 – Handles fetching data efficiently, optimized for performance.

Instead of your controllers directly manipulating the database, CQRS introduces Commands and Queries, leading to better scalability, maintainability, and performance.

Why Use CQRS? 🤔

Performance Boost – Read queries can be optimized separately from writes.
Scalability – Scale queries differently from commands based on load.
Security – Easier to manage permissions separately for reading and writing.
Flexibility – Ideal for microservices, event sourcing, and complex systems.

Sounds fancy? Let’s build something cool with it! 💻

What Are We Building? 🏗️

We’ll create a simple Product Management API using CQRS without MediatR. While MediatR's built-in request/handler pattern simplifies things, we’ll manage commands and queries via explicit services which gives us full control and keeps things explicit.

🔹 Command Service → Handles Create, Update, and Delete (Writes)
🔹 Query Service → Handles Fetching Data (Reads)

👉 Want to see how MediatR fits into the picture? Stay tuned for the next blog! 😉

Step 1: Create a New ASP.NET Core Web API

Run the following commands to create the project:

Add Entity Framework Core:


Step 2: Define the Product Model

Create a folder /Models and add Product.cs



Now, create Data/AppDbContext.cs:


Register the DB context in Program.cs:


Step 3: Implement CQRS

Command Service (Handling Writes)

Create a Services/Commands/ProductCommandService.cs:


Query Service (Handling Reads)

Create Services/Queries/ProductQueryService.cs: 


Step 4: Hooking Services into the Controller
Register Services in Program.cs

Create the Controller
Modify Controllers/ProductController.cs: 




Step 5: Run & Test the API

Run the app: 

Test the Endpoints

1️⃣ Create a Product 

2️⃣ Get All Products  

Key Takeaways

  • Clear Separation of Concerns – Commands handle writes, and queries handle reads, making the code-base more structured and maintainable.
  • Improved Performance – Read and write operations can be optimized separately, reducing unnecessary database locks and improving response times.
  • Scalability – You can scale query and command services independently based on system load, making the architecture more adaptable.
  • Better Maintainability – The modular structure makes it easier to update features, add new queries, or modify command logic without affecting other parts of the system.
  • Easier to Extend – Additional functionalities like event-driven architecture, caching, or distributed processing can be integrated seamlessly.

This approach keeps your application clean, scalable, and ready for future enhancements

See you in the next blog, where we’ll dive into implementing CQRS with MediatR! Until then, keep coding, keep learning, and stay awesome! 😀


Comments

Popular Posts