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.
- β 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
- ASP.NET Core 7
- Entity Framework Core
- MediatR
- SQL Server (or compatible DB)
- Swagger (Swashbuckle)
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
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: 1pageSize
(int) β default: 10sortBy
(string) β e.g.,name
,price
ascending
(bool) β true/falsefilterName
(string)minPrice
,maxPrice
(decimal)
- .NET 7 SDK
- SQL Server (or SQL Server Express/localdb)
- 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