Table of Contents

A Quick Note

Last time, we talked about dependency injection with .NET and how it relates to inversion of control and other jargon like dependency inversion. Today, let’s talk about a framework that incorporates these design principles—Clean Architecture. If you are not familiar with dependency injection, I recommend checking out my previous post:

Dependency Injection - Understand with .NET

I will be using .NET to explain the architecture, but you do NOT need to know anything about C# or .NET to follow along.

Without Clean Architecture?

Before we dive deep into the Clean Architecture framework, I want to briefly discuss what most people, including myself, often do when designing a service. Normally, we default to using Layered Architecture, where we separate the service into the presentation layer, business logic, and data access layer, as illustrated in the diagram below.

layered.drawio.png

While there isn’t anything inherently wrong with this approach, we will notice that the presentation layer directly depends on the business logic layer, which in turn directly depends on the data access layer. In other words, it does not follow the dependency inversion design principle we discussed in the previous post. With this “coupled” structure, testing and scaling can become more difficult.

An Alternative Approach — Clean architecture

Clean Architecture was first proposed in 2017 in a book called Clean Architecture: A Craftsman's Guide to Software Structure and Design by Robert C. Martin (“Uncle Bob”). It can be summarized by this onion-like structure below:

clean.drawio.png

Let’s break down what each layer do: