DEV Community

mohamed Tayel
mohamed Tayel

Posted on • Updated on

Part 1: What is Clean Architecture?

Understanding Clean Architecture

I would like to express my gratitude to Gill Cleeren, Pluralsight Author, for his excellent course on ASP.NET Core Clean Architecture. His insights and teachings have greatly deepened my understanding of Clean Architecture, a software design philosophy introduced by Robert C. Martin (Uncle Bob) that focuses on creating systems that are easy to understand, flexible, and maintainable. The course emphasizes the importance of separation of concerns, ensuring that the business logic of an application is decoupled from its dependencies, such as frameworks, databases, and user interfaces. Thank you, Gill, for your valuable contribution!.

Definition and Principles of Clean Architecture

Definition:
Clean Architecture is a layered architecture that organizes code into a set of concentric circles, each representing different layers of the application. These layers include entities, use cases, interface adapters, and frameworks/drivers. The core idea is that the inner layers should be independent of the outer layers, making the system more modular and easier to test.

Principles:

  1. Separation of Concerns: Each layer has a distinct responsibility, which helps to manage complexity and improve code readability.
  2. Dependency Rule: Source code dependencies can only point inward. Nothing in an inner circle can know anything at all about something in an outer circle.
  3. Independent of Frameworks: The architecture should not depend on any external frameworks. This allows the core of the application to remain stable even if frameworks change.
  4. Testability: Business rules can be tested independently of UI, database, and other external dependencies.
  5. Flexibility and Maintainability: The system can evolve without significant changes to the overall structure, making it easier to maintain and extend.

Benefits of Using Clean Architecture in Software Development

  1. Improved Testability: By decoupling the business logic from external dependencies, it becomes easier to write unit tests and achieve higher test coverage.
  2. Flexibility: Changes to one part of the system (e.g., replacing a database or a web framework) can be made with minimal impact on other parts.
  3. Maintainability: Clear separation of concerns and modularity make the codebase easier to understand, maintain, and extend.
  4. Reusability: Business logic can be reused across different projects or components, reducing redundancy.
  5. Scalability: Clean Architecture facilitates scaling the application, both in terms of handling increased load and adding new features.

Key Components and Layers in Clean Architecture

  1. Entities: Represent the core business objects of the application. They encapsulate the most general and high-level rules. They are typically rich domain objects containing business rules and logic.

  2. Application Core: Contain the application-specific business rules. They define the jobs the application needs to perform, encapsulating and implementing all the use cases of the system.

  3. infrastructure: Convert data from the format most convenient for the use cases and entities to the format most convenient for external agencies such as databases, web, UI, or external services.

  4. User interface: Include the UI, database, web frameworks, and any other external tools or delivery mechanisms. These are the outermost layer and have the least amount of code.

Diagrams to Illustrate Concepts

To better understand Clean Architecture, let’s look at the corresponding diagram:

Diagram:

Understanding Clean Architecture

In this diagram, the entities are the core business objects that define the essential properties and behaviors. The Application Core encapsulate the application-specific business logic, defining what the application should do. The infrastructure handle the interaction between the use cases and the external world, such as web requests and database access. Finally, the User interface layer includes the actual implementation of the web framework, database, and other external dependencies.

By organizing your application in this manner, you can achieve a clean separation of concerns, making your codebase more robust, testable, and maintainable.

Conclusion

Understanding and implementing Clean Architecture can significantly enhance the quality and longevity of your software projects. By adhering to its principles, you ensure that your application remains flexible, maintainable, and scalable, ready to adapt to future requirements and technologies. Stay tuned for the next part of our series, where we will dive into the concept of microservices and how they can be integrated with Clean Architecture in .NET 8.

Top comments (19)

Collapse
 
mkunikow profile image
Michal Kunikowski

There is also hexagon architecture. Maybe you can compere.
I can add one article for reference (sorry for medium)

"Hexagonal Architecture, there are always two sides to every story"
medium.com/ssense-tech/hexagonal-a...

jmgarridopaz.github.io/content/art...

Collapse
 
litlyx profile image
Antonio | CEO at Litlyx.com

MVVM, Clean architeture is a standard for very large and complex code bases that need a structure. I used when i was a enloyee in a big company. I rember that the barrier was really big for a person who is not a "at his prime" so sometimes scares the people. But is the optimal solution for large scale codebases.
Good work.
Antonio, CEO at Litlyx

Collapse
 
moh_moh701 profile image
mohamed Tayel

thanks

Collapse
 
hlexnc profile image
HlexNC

Can you next time provide an example of project with clean architecture? Thanks.

Collapse
 
moh_moh701 profile image
mohamed Tayel

example using .net core API
github.com/mohamedtayel1980/clean-...

Collapse
 
moh_moh701 profile image
mohamed Tayel

soon

Collapse
 
naviny0 profile image
Navin Yadav

ok

Collapse
 
james_mcmenamin_5ac505935 profile image
James McMenamin

For mobile and large codebase I have switched to a Vertical architecture + clean design approach nowadays just easier to manage the base.

Collapse
 
aliadelnour profile image
Ali Nour Al Din

منور يا هندسة💕

Collapse
 
nehemiahj profile image
Nehemiah Jeyakumar

Great article

Collapse
 
moh_moh701 profile image
mohamed Tayel

thanks

Collapse
 
z2lai profile image
Info Comment hidden by post author - thread only accessible via permalink
z2lai • Edited

Your diagram looks more like rectangles instead of circles 😀. I've always wondered, is there an actual difference in what the circlular clean architecture diagrams depict vs the non-circular diagrams?

FYI, if your article was assisted by AI, you should disclose that or tag it appropriately.
dev.to/devteam/guidelines-for-ai-a...

Collapse
 
ahmedagamy profile image
Top Developers

Thanks for this great article but I have some questions that need to clarify.

1- Entities: Represent the core business objects of the application. They encapsulate the most general and high-level rules. They are typically rich domain objects containing business rules and logic.
I believe entities shouldn't have any business logic and if you need to have some validation rules we can keep it in use cases or in a helper class to validate data before saving, is that right ?

2- User interface: Include the UI, database, web frameworks, and any other external tools or delivery mechanisms. These are the outermost layer and have the least amount of code.
the user interface layer is just part of the framework & drivers layer and this layer includes (DB - Devices - Web - UI - External Interfaces).

Collapse
 
marcello_h profile image
Marcelloh

How would a business rule be applied on an entity-field (if the only way is in)?

Collapse
 
moh_moh701 profile image
mohamed Tayel

Let's say we have an entity called order and i want make sure when set quantity it must >0 we will do the following
public class Order
{
public int Quantity { get; private set; }

public void SetQuantity(int quantity)
{
    if (quantity <= 0)
    {
        throw new ArgumentException("Quantity must be greater than zero.");
    }
    Quantity = quantity;
}
Enter fullscreen mode Exit fullscreen mode

}
as you see the i make private set and create method for validate the quantity
This encapsulates the business rule within the entity, making it easier to maintain and understand

Collapse
 
marcello_h profile image
Marcelloh

In that case, the business rule is "hidden" in the entity, which is not in the Application Core.

Thread Thread
 
moh_moh701 profile image
mohamed Tayel

sorry, I misunderstood your question. To clarify, we can add business rule in APP core

public interface IOrderService
{
void SetOrderQuantity(Order order, int quantity);
}

// Application Core - Service Implementation
public class OrderService : IOrderService
{
public void SetOrderQuantity(Order order, int quantity)
{
if (quantity <= 0)
{
throw new ArgumentException("Quantity must be greater than zero.");
}
order.SetQuantity(quantity);
}
}

Some comments have been hidden by the post's author - find out more