Buddhika Nelum
3 min readSep 30, 2022

--

Clean Architecture Simplified. (.NET Core) — With Sample Project.

Hello everyone, Today I’m going to explain how to structure your .net core project in a clean and well-structured way using multiple layers. I’ve been using clean architecture for the past 3 years in different ways but My personal preference is Jason Taylor's clean architecture solution.

GitHub: https://github.com/jasontaylordev/CleanArchitecture

First of all, let’s talk about why we need to structure our project in such a way. As developers, we need to make sure our project never implements any code smells and we need to know what to use where. Otherwise, it will be very difficult and time-consuming to find bugs and even add new features and eventually end up with an unmanageable project. Even if you're developing a simple project for your college assignment or even doing a complex and large-scale project for a client, developers should know these things as common sense.

The clean architecture was introduced by Robert C. Martin also known as Uncle Bob in his blog. http://cleancoder.com

This is the definition of clean architecture from the official Microsoft website.

https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures

Clean architecture puts the business logic and application model at the center of the application. Instead of having business logic depend on data access or other infrastructure concerns, this dependency is inverted: infrastructure and implementation details depend on the Application Core.

Let’s simplify this,

The solution can be structured in different ways according to the complexity of the project. Today I’m gonna show the simplest way to do this for beginners so this can be used for your college projects or any small to medium-sized projects.

There are 4 main layers.

  • Application
  • Domain
  • Infrastructure
  • Presentation

So why is the project split like this?

As you can see in the above image, the Domain layer is at the very core of the project. This is the backbone of our project which should not have any external dependencies from other projects. Typically this layer contains any enterprise-wide things like types, entities, enums, constants, exceptions, etc.

The application layer contains application-specific things such as interfaces, abstractions, mappers, etc. This layer should only depend on the domain layer and should not contain any implementations.

The Infrastructure layer contains all the implementations of the services you defined as interfaces/abstract classes in the application core layer. For a simple example, you can have an ICustomerService interface with a GetCustomer() method defined in the application core layer and write the code/logic on how to get customers on CustomerSerivce in the infrastructure layer. So you can use these abstractions with dependency injection in your code.

The presentation layer can be a UI or web API. This project depends on the application layer and infrastructure layer. And another important thing is it should depend on the infrastructure layer only for service registration in a startup.cs class. For no other reason, you should use the infrastructure layer in the presentation layer.

So by doing this we are achieving a very safe and clean project solution as you can see below.

Here I have created a simple example project with clean architecture. you can use it for your future projects. Use the GitHub link below

Checkout my other articles as well.

Happy Coding :)

--

--