Get In Touch

Build a Clean CRUD API with .NET, MediatR, and NSwagLeaf

Project

Article Information’s

Author

Usama Irfan

Category

Web Development

Date

Jun 14, 2025

Introduction

In today’s fast-paced development environment, building scalable and maintainable APIs is essential. ASP.NET Core has emerged as a powerful framework for creating modern web APIs, and when combined with Clean Architecture principles, MediatR for CQRS, Entity Framework Core, and Cosmos DB, it becomes a robust foundation for real-world applications.

In this guide, we’ll walk through the process of building a clean and well-structured CRUD API for a simple Todo app using .NET 8, MediatR, EF Core with Cosmos DB, and NSwag for automatic Swagger documentation. Whether you're creating enterprise-grade microservices or looking to enforce separation of concerns in your architecture, this article will help you set up a production-ready foundation with best practices.

We'll cover:

  • Setting up a Clean Architecture solution with separate layers (Domain, Application, Infrastructure, API)

  • Using CQRS with MediatR to handle Create, Read, Update, and Delete operations

  • Connecting to Azure Cosmos DB using Entity Framework Core

  • Exposing a fully-documented REST API using NSwag (Swagger UI)

Let’s dive in and build a clean, scalable .NET API from scratch.