Keep rules independent of frameworks, and split one deployable app into clear modules before you split it into services.
These are two ideas that share one goal: keep the parts of your code separate, so a change in one place does not break ten others.
Picture rings, like an onion. In the middle are your entities (the core business objects, such as an Order) and the use cases (what the app does: place an order, cancel an order). Around them are adapters that translate to the outside world: web controllers, database code, message handlers. On the outside are the tools themselves: the framework, the database product, the UI.
The one rule that holds it together is the dependency rule: source code can only point inward. The business rules never import the database or the web framework. When the rules need to save something, they call a small interface that they own (a "port"), and the database code on the outside implements it. This trick is called dependency inversion.
A monolith does not have to be a mess. In a modular monolith you divide the one app into modules by business area (Orders, Billing, Shipping). Each module has a small public interface and keeps its internals and its tables private. Other modules may only use the interface. Calls between modules are plain in-process calls, so they are fast and can share one transaction when really needed.
| Style | Main picture | Core idea |
|---|---|---|
| Layered (N-tier) | A stack: UI, business, data | Each layer talks to the one below. The business layer often ends up depending on the data layer. |
| Hexagonal (ports and adapters) | A hexagon with plugs | The core defines ports. Adapters plug in from outside. |
| Onion | Concentric rings | Domain at the centre, dependencies point inward. |
| Clean architecture | Four rings | The same dependency rule, with named rings (entities, use cases, adapters, frameworks). |
These styles are close relatives. They all invert the dependency on the database, so the business rules sit in the middle.
| Rule | Meaning | Why it matters |
|---|---|---|
| Public interface | Other modules call a small, stable set of functions. | Everything else can change without notice. |
| Private data | Only the module's own code touches its tables. | Prevents hidden coupling through the database. |
| No cyclic dependencies | Order calls Billing, but Billing does not call Order back. | Cycles make modules impossible to split later. |
| Events for side effects | A module announces 'order placed' and others react in-process. | Keeps modules from knowing each other. |
| Enforced by tests or tools | A build check fails on a forbidden import. | Rules written only in a document get broken. |
A teaching sketch with a fixed example app. The counts (3 dependencies, 7 boxes against 1) are illustrative, not measurements. Real systems have many more modules, and boundaries are checked by tests or tools.
| Term | Meaning in easy words |
|---|---|
| Dependency rule | Source code dependencies may only point inward, towards the business rules |
| Entity | A core business object with its rules, such as Order or Money |
| Use case | One thing the app does, such as 'place an order' |
| Port | An interface owned by the business rules that the outside must implement |
| Adapter | Code that connects a port to a real tool, such as a database |
| Dependency inversion | High-level rules depend on an interface, not on low-level details |
| Module | A part of the app with a public interface and private internals |
| Public interface | The small set of calls other modules are allowed to use |
| Bounded context | A business area with its own words and rules (from Domain-Driven Design) |
| Coupling | How much one part needs to know about another |
| Architecture test | An automatic check that fails the build if a forbidden dependency appears |
| Choice | We gain | We lose |
|---|---|---|
| Clean architecture rings | Testable rules, swappable tools | Extra indirection and boilerplate |
| Direct database calls in rules | Less code at the start | Painful to test and to change tools |
| Modular monolith | Clean borders with simple operations | Whole app deploys together and scales together |
| Microservices | Independent deploys and scaling | Network failures, data splitting, more operations |
| Shared tables across modules | Easy joins and reports | Hidden coupling, so modules cannot change alone |
| Strict module rules | Fewer surprises later | Slower feature work at first |