Clean Architecture — ASP.NET Core API using Partitioned Repository Pattern with Azure Cosmos DB
Discussing a GitHub starter project to build a web API using Partitioned Repository Pattern with Azure Cosmos DB, ASP.NET Core, and Clean Architecture.
UPDATE April 10, 2022: all projects in the GitHub repo have been upgraded to .NET 5.

This article describes the GitHub project that can be used as a starting point to work with:
- Clean Architecture (Onion Architecture)
- ASP.NET Core 3.1
- Azure Cosmos DB .NET SDK V3
- Repository Design Pattern
- Partition Key and Partitioned Repository
Popular features also supported in the project include:
- Cosmos DB database initial creation and data seeding
- Query data using Parameterized Query, LINQ and IQueryable, Specification Pattern in Cosmos DB
- REST API with OData support and Swagger UI
- MediatR Command/Query pattern
- MediatR pipeline behaviour for exception handling
- FluentValidation for validation
Please see a full updated feature list in the GitHub repo. If you want to dive straight into the code, please see GitHub repository Clean Architecture with partitioned repository pattern using Azure Cosmos DB. The GitHub repository is work in progress. I will be publishing updates as I add more pieces to it.
Clean Architecture — Introduction
Microsoft has a library of amazing architecture documentations, one of which is called Architect Modern Web Applications with ASP.NET Core and Azure, which is also an e-book written by Steve Smith, aka, “Ardalis”. Clean Architecture is well explained in this e-book if you are interested in the great details.
At the very high-level, Clean Architecture attempts to follow architectural principles like Separation of concerns, Dependency inversion, Persistence ignorance Bounded contexts, etc.. These principles may sound too abstract, but really, they aim to make it easy for the software engineers like you and me to do right, and make it hard for us to do wrong. One of my favorite diagram (see above) from this e-book is the onion view of the architecture layers.
From this diagram, you can tell the development cycle, starting from the core to the outer layers, becomes:
- Define business entities (business requirement)
- Define business services (business requirement)
- Develop infrastructure (implementation)
- Develop UI (presentation)
This cycle allows the software engineers to meet the business requirements first by defining everything in abstraction. Any business requirement revision can be achieved at the early phase easily, compared to revising fully implemented code.
Azure Cosmos DB .NET SDK V3
When I searched in the GitHub community, I could not find a Clean Architecture repo that uses the newest .NET SDK V3 for Cosmos DB. I love the new features in the .NET SDK V3 compared to V2, particularly support for Stream APIs, support for Change Feed processor APIs.
So I decided to take Mr. Ardalis’ starter project Clean Architecture, and
- add support for Cosmos DB using .NET SDK V3
- add partitioning to the repository design pattern
Code Walkthrough
Application Core

The Core project only defines the domain models and the business logic in abstraction. That’s why the Core project has minimal dependencies and it never would depend on the Infrastructure or Web projects.
IRepository.cs
This interface defines the default database interactions. Notice generics T is defined, so that any type specific repositories can just inherit this interface without duplicating the method definitions. See IToDoItemRepository.cs for example.






