Clean Architecture Series - 1. Starting out

The 2nd hardest part is starting a project, the 1st is

Ian Bowyer

Welcome to my blog series on mastering clean architecture with the MediatR library!

In the ever-evolving world of software development, maintaining a clean and scalable architecture is crucial for building robust applications. This series will delve into the principles and practices of clean architecture, leveraging the powerful MediatR library to streamline your workflows.

Throughout this journey, we'll explore the essential elements of setting up your solution, implementing MediatR for command and query handling, adding robust validation mechanisms, and much more. By the end, you'll be equipped with the knowledge and tools to create applications that are not only maintainable but also resilient to change.

Get ready to enhance your coding skills and dive deep into the world of clean architecture. Let’s make your codebase cleaner, one step at a time.

Stay tuned and happy coding!

  • Starting the solution
  • The basics of Mediatr
  • Adding Validation
  • Adding some Domains
  • Domain Types and using the Strongly Typed IDs Library
  • Adding some infrastructure - EF Core
  • Some useful Utilities to write

Introduction to clean architecture and its importance

Clean architecture is essential because it separates concerns, making your code more modular, testable, and maintainable. This allows for easier scaling and adapting to changes in the future. The basic layers are:

  • Presentation
  • Application
  • Domain
  • Infrastructure

These are often shown in a onion type diagram:

  • Presentation Layer - Handles everything the user interacts with. The API Endpoints
  • Application Layer - Orchestrates the application's use cases and business logic. Commands and Queries
  • Domain Layer - Core business logic and rules. Rules such as CustomerName must have a value.
  • Infrastructure Layer - Handles external dependencies. Such as calling a Database

Together, these layers ensure a clean separation of concerns, making your application modular, maintainable, and scalable.

Setting up the initial project

There's different ways to setup the project.

  1. You can have one web project with all the layers🤮
  2. You can have projects for each layer 🤨
  3. You can have projects for each layers and have some common code to try and keep things cleaner 😊

I have opted to do option 3. There is some code which just make things easier but can clutter your main project. These could easily be moved to NuGet packages for your organisation to reuse on projects. 

Ok Lets Go!


  • Create a new ASP.NET Core Web API
    • New > Project > ASP.NET Core Web API
    • Project Name : CleanArchitecture.Api
    • I'm using .NET 8.0 (currently LTS)
    • HTTPS - ✔️
    • Use Controllers - ✔️

This should get you something like 

    • Remove the code files
      WeatherForecastController.cs
      WeatherForcast.cs
    • Also comment out the following launch settings
      "launchUrl": "weatherforecast",
    • Also remove weatherforecast from the CleanArchitecure.Api.http so that the line is
      GET {{CleanArchitecture.Api_HostAddress}}/

Now Lets create a class library for each layer

  • CleanArchitecture.Application
  • CleanArchitecture.Domain
  • CleanArchitecture.Infrastructure

Feel free to remove the Class.cs from each one. We will come back to these project later. 

I think it is also worth creating a few folders for:

  • Common
    • Right Click on Solution > Add Folder. 
    • Name it Common
  • Tests
    • Right Click on Solution > Add Folder. 
    • Name it Tests
  • Utilities
    • Right Click on Solution > Add Folder. 
    • Name it Utilities

Add some references.

Different layers can communicate between themselves but they can not jump between layers. Lets set that up now

  • CleanArchitecture.Api> Right Click Dependencies > Add Project Dependency
    • Tick CleanArchitecture.Application
    • Tick CleanArchitecture.Infrastructure
  • CleanArchitecture.Application> Right Click Dependencies > Add Project Dependency
    • Tick CleanArchitecture.Domain

Conclusion

We've successfully laid the groundwork for our clean architecture journey by creating a well-organized solution structure with clearly defined layers: CoreInfrastructure, and Presentation (API). This initial setup is crucial, as it forms the backbone of our application, ensuring a modular and maintainable codebase.

By organizing the code within these layers, we have set ourselves up for success in the upcoming posts. Each layer will handle its specific responsibilities, allowing us to build a robust and scalable application as we delve into topics such as implementing MediatR, adding validation, and more.

This initial organization will streamline our development process and make it easier to manage and extend the application as we progress. With this solid foundation in place, we're ready to dive deeper into the intricacies of clean architecture.

Stay tuned for the next post where we will explore the basics of MediatR and begin implementing our first request and handler!

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.

TimsothyCed
TimsothyCed - Friday, May 29, 2026

Your formatting choices and the overall layout of this article make it an absolute pleasure to read, ensuring that every important detail is highlighted clearly for those who might just be skimming through the text quickly. <a href=https://www.cactuspark.fr/>casino en ligne retrait immediat</a>

Dichaelkeedy
Dichaelkeedy - Friday, May 29, 2026

I am definitely going to save this link for future reference, because the simple and focused explanations provided here are going to be very useful whenever I need a quick and reliable reminder about how this all works. <a href=https://coupemonde.fr/sites-paris-sportifs/>bookmaker hors arjel</a>

GichardSof
GichardSof - Wednesday, May 27, 2026

I like how this post presents the ideas in a balanced and easygoing way that makes the discussion more enjoyable overall. <a href=https://gerardzwanenburg.nl/>https://gerardzwanenburg.nl/</a>

OLaneMus
OLaneMus - Tuesday, May 26, 2026

The level of detail provided in this write-up is genuinely valuable, and it is clear that a significant amount of thought and preparation went into ensuring the final result would be this useful to the audience. <a href=https://beautymodel.club>фото-кастинг бьютимоделей</a>

LewisSmomA
LewisSmomA - Friday, May 22, 2026

I really like how this post manages to keep the content valuable while still maintaining a relaxed and approachable tone, because that combination helps make the discussion more pleasant and accessible for a wider audience. <a href=https://startartfair.com/>porno latin</a>

LewisSmomA
LewisSmomA - Thursday, May 21, 2026

Hey! I'm at work surfing around your blog from my new iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Carry on the superb work! <a href=https://rlifiles.com/>turkce altyazi porno.</a>

LhaneMus
LhaneMus - Thursday, May 21, 2026

Hi there! Do you use Twitter? I'd like to follow you if that would be okay. I'm absolutely enjoying your blog and look forward to new updates. <a href=https://www.redbynight.net/>porno xxxl</a>

EarnestflueS
EarnestflueS - Thursday, May 21, 2026

Admiring the dedication you put into your blog and detailed information you provide. It's great to come across a blog every once in a while that isn't the same out of date rehashed material. Excellent read! I've saved your site and I'm adding your RSS feeds to my Google account. <a href=https://picch-project.org/>georgian porno</a>

GichardSof
GichardSof - Thursday, May 21, 2026

This is very interesting, You're a very skilled blogger. I've joined your feed and look forward to seeking more of your great post. Also, I have shared your web site in my social networks! <a href=https://melbet.org.az/>maloletki porno</a>

TimsothyCed
TimsothyCed - Wednesday, May 20, 2026

May I simply say what a comfort to find someone who actually knows what they are talking about online. You certainly understand how to bring an issue to light and make it important. More and more people need to read this and understand this side of the story. I was surprised that you're not more popular since you most certainly have the gift. https://oncology.com.ua/rezystor-hriie-kondensator-movchyt-obydva.html

OLaneMus
OLaneMus - Tuesday, May 19, 2026

Hi fantastic blog! Does running a blog similar to this require a great deal of work? I have virtually no understanding of programming but I had been hoping to start my own blog soon. Anyhow, if you have any recommendations or tips for new blog owners please share. I know this is off topic but I simply had to ask. Thank you! https://www.google.ws/maps/d/edit?mid=1jTaHqaYfD4Q_Bj8x2HPglHvrvV22JGo

GichardSof
GichardSof - Tuesday, May 19, 2026

Hello mates, good paragraph and nice urging commented at this place, I am truly enjoying by these. <a href=https://archigram.net/>บาคาร่า</a>

EarnestflueS
EarnestflueS - Monday, May 18, 2026

I blog often and I seriously appreciate your information. Your article has truly peaked my interest. I am going to take a note of your blog and keep checking for new details about once per week. I opted in for your Feed as well. <a href=https://www.abysport.com/>casino retrait instantane</a>

FobertBeelp
FobertBeelp - Thursday, May 7, 2026

I loved as much as you will receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this increase. <a href=https://www.versobank.com/>kasiino tervitusboonus</a>