Skip to content

πŸ› οΈ CQRS + MediatR Design Pattern with EF Core - ASP.NET Core Demo This is a demo project showcasing the CQRS (Command Query Responsibility Segregation) design pattern implemented using MediatR, Entity Framework Core, and ASP.NET Core Web API. The project demonstrates how to cleanly separate command and query responsibilities, improve scalability,.

Notifications You must be signed in to change notification settings

sandeeppaldotnet/CQRS-Mediatr-Design-Pattern-WithEFCoreDemo-Aspnetcore

Repository files navigation

πŸ› οΈ CQRS + MediatR Design Pattern with EF Core - ASP.NET Core Demo

This is a demo project that demonstrates how to implement the CQRS (Command Query Responsibility Segregation) design pattern using MediatR, Entity Framework Core, and ASP.NET Core Web API. It's structured to show best practices for clean, maintainable, and scalable backend development in .NET.

πŸ“Œ Features

  • βœ… CQRS implementation with MediatR
  • βœ… Clean separation of command and query responsibilities
  • βœ… CRUD operations on Product entity
  • βœ… Pagination, sorting, and filtering support
  • βœ… Entity Framework Core for data persistence
  • βœ… Swagger UI for API testing and exploration

πŸš€ Tech Stack

  • ASP.NET Core 7
  • Entity Framework Core
  • MediatR
  • SQL Server (or compatible DB)
  • Swagger (Swashbuckle)

πŸ“ Project Structure

MediatRWithEFCoreDemo/ β”‚ β”œβ”€β”€ Controllers/ β”‚ └── ProductsController.cs # API Endpoints β”‚ β”œβ”€β”€ Features/ β”‚ └── Products/ β”‚ β”œβ”€β”€ Commands/ # Create, Update, Delete Commands β”‚ └── Queries/ # GetOne, GetAll Queries β”‚ β”œβ”€β”€ Models/ β”‚ └── Product.cs # Domain Model β”‚ β”œβ”€β”€ Data/ β”‚ └── AppDbContext.cs # EF Core DbContext β”‚ └── Program.cs / appsettings.json # App setup and configuration

πŸ“‘ API Endpoints

Method Route Description
GET /api/products Get all products (with filters)
GET /api/products/{id} Get product by ID
POST /api/products Create new product
PUT /api/products/{id} Update product
DELETE /api/products/{id} Delete product

GET /api/products supports query parameters:

  • pageNumber (int) β€” default: 1
  • pageSize (int) β€” default: 10
  • sortBy (string) β€” e.g., name, price
  • ascending (bool) β€” true/false
  • filterName (string)
  • minPrice, maxPrice (decimal)

βš™οΈ Getting Started

Prerequisites

  • .NET 7 SDK
  • SQL Server (or SQL Server Express/localdb)

Setup Instructions

  1. Clone the Repository
    git clone https://github.com/sandeeppaldotnet/CQRS-Mediatr-Design-Pattern-WithEFCoreDemo-Aspnetcore.git
    cd CQRS-Mediatr-Design-Pattern-WithEFCoreDemo-Aspnetcore
    
    Update Connection String

Modify appsettings.json to point to your SQL Server instance.

Apply Migrations & Create DB

bash Copy Edit dotnet ef database update Run the Project

bash Copy Edit dotnet run Open Swagger Visit https://localhost:{port}/swagger to test the API.

πŸ“¦ Sample Payloads Create Product (POST) json Copy Edit { "name": "Laptop", "price": 799.99, "description": "Powerful laptop for developers" } Update Product (PUT) json Copy Edit { "id": 1, "name": "Laptop Pro", "price": 999.99, "description": "Updated version with more features" } 🀝 Contributing Feel free to fork this repo, raise issues, and submit pull requests. Contributions are welcome!

πŸ“„ License This project is licensed under the MIT License.

πŸ™‹β€β™‚οΈ Author Sandeep Pal GitHub: @sandeeppaldotnet

About

πŸ› οΈ CQRS + MediatR Design Pattern with EF Core - ASP.NET Core Demo This is a demo project showcasing the CQRS (Command Query Responsibility Segregation) design pattern implemented using MediatR, Entity Framework Core, and ASP.NET Core Web API. The project demonstrates how to cleanly separate command and query responsibilities, improve scalability,.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages