The Secret Behind a Clean Architecture
You've likely heard about Clean Architecture many times, yet you may not see it often in the projects you work on. As with any architectural approach, there are cases where it's a perfect fit and others where it's unnecessary. The benefits of Clean Architecture are undeniable, and this is no accident. The secret behind Clean Architecture lies in its strong focus on business, built upon two main concepts: Entities and the Dependency Rule. Let's examine each of these in greater detail.
Entities
As Uncle Bob says in his book "Clean Architecture," an entity is an object that embodies a small set of Critical Business Rules operating on Critical Business Data. But what do these two concepts mean?
A Critical Business Rule is a rule that makes or saves the business money. Think of these as rules that would make or save money even without software, and the data used by these rules is called Critical Business Data. When starting a new development, look at the business with these two concepts in mind. By identifying them, you'll uncover the most important aspects of the business, allowing you to prioritize them above all else. Remember, the Entities are where the money is—that's why an Entity is pure business and nothing else.
The Dependency Rule
The Dependency Rule enforces that more important modules can only be imported by modules in less important layers. This rule protects the Entities from any less critical aspect of the development. That's why elements such as frameworks, controllers, and even use cases are not at the center of the architecture:
It's important to understand that these layers are just guidelines. You can create as many as needed, but the Entities and the Dependency Rule will always remain the cornerstones of the architecture. If a new requirement necessitates changes in an entity, it may impact every layer (in the worst-case scenario). Conversely, if a change is made at the framework level, it won't affect the core business logic.
The business as the center of everything
This is the secret of Clean Architecture: you always put the business before anything else. The business doesn't care about which framework or database you use; its only concern is ensuring the investment is profitable and costs are minimized. We meet these objectives by putting the business at the center of everything, enforcing our entities to depend on nothing else through the dependency rule.
Clean Architecture shines when you've successfully identified the critical business rules and data. As developers, we often fall in love with frameworks, libraries, ORMs, or languages. However, the truth is that these are just tools to an end—and that end will always be the business. Our responsibility is to make the most of these tools in service of the business.