@@ -61,35 +61,55 @@ In essence, it’s about making your application independent of external systems
6161
6262The most abstract policies define core business rules, while the least abstract ones handle I/O operations.
6363Being closer to implementation details, less abstract policies are more likely to change.
64- A layer represents a collection of components expressing policies at the same level of abstraction.
64+ ** Layer ** represents a collection of components expressing policies at the same level of abstraction.
6565
66- The concentric circles represent boundaries between different layers.
66+ Concentric circles represent boundaries between different layers.
6767The meaning of the arrows in the diagram will be discussed [ later] ( #dependency-rule ) .
6868For now, we will focus on the purpose of the layers.
6969
7070## Layered Approach
7171
7272![ #gold] ( https://placehold.co/15x15/gold/gold.svg ) ** Domain Layer**
7373
74- - The core of the application, containing ** entities** , ** value objects** , and ** domain services** that encapsulate
75- critical business rules — fundamental principles or constraints that define how the business operates and delivers
76- value.
77- In some cases, these rules can be seen as mechanisms that create the product's value independently of its
78- software implementation.
79- Changing them often reflects a change in the business itself.
80- - It establishes a ** ubiquitous language** — a consistent terminology shared across the application and domain.
81- This is the language you can speak with managers.
82- - It's the most stable and independent part of the application.
83- - Domain services originally represent operations that don't naturally belong to a specific entity.
84- In projects with anemic domain models — where entities hold data but no behavior — domain services may also include
85- logic that would otherwise reside inside those entities.
74+ - ** Domain model** is a set of concepts, rules and behaviors that define what business (context) is and how it operates.
8675
87- > [ !NOTE]
88- > The Domain layer may also include ** aggregates** (groups of entities that must change together as a single unit,
89- > defining the boundaries of transactional consistency) and ** repository interfaces** (abstractions for manipulating
90- > aggregates).
91- > While these concepts aren't implemented in the project's codebase, understanding them can deepen your knowledge of
92- > DDD.
76+ It is expressed in a ** ubiquitous language** — a consistent terminology shared by developers and domain experts.
77+
78+ Domain layer implements domain model in code; this implementation is often called domain model.
79+ - The strictest domain rules are ** invariants** — conditions that must always hold true for the model.
80+
81+ Enforcing invariants means maintaining data consistency in the model.
82+
83+ This can be achieved through ** encapsulation** , which hides internal state and couples data with behavior.
84+ - Building blocks of domain model are (not limited to these):
85+ - ** value objects** — smart business types (no identity, immutable, equal by value).
86+ - ** entities** — business objects (have identity and lifecycle, equal by identity).
87+ - ** domain services** — containers for behavior that has no place in the components above.
88+ - Other domain model building blocks, unused in this project but important for deeper DDD:
89+ - ** aggregates** — clusters of entities (1+) that must change together as a single unit,
90+
91+ managed exclusively through their root, defining boundaries of transactional consistency.
92+ - ** repositories** — abstractions emulating collections of aggregate roots.
93+ - Domain model lies on a spectrum from anemic to rich.
94+ - ** anemic** — simple data types, entities are just data holders, rules and behaviors live outside.
95+ - ** rich** — value objects and entities encapsulate data and rules;
96+
97+ invariants are enforced internally, so the model itself prevents invalid states.
98+
99+ For components: anemic means no behavior within, rich — the contrary.
100+ - Domain services originally represent operations that don't naturally belong to a specific entity or value object.
101+
102+ But in projects with anemic entities, they can also contain logic that would otherwise be in those entities.
103+ - In the early stages of development when the domain model is not yet clearly defined,
104+
105+ I'd recommend keeping entities flat and anemic, even though the latter weakens encapsulation.
106+
107+ Once the core logic is well established, some entities can, as aggregate roots, become non-flat and rich.
108+
109+ This best enforces invariants but can be tricky to design once and for all.
110+
111+ - Prefer rich value objects early, freeing entities and services from an excessive burden of local rules.
112+ - Consider domain layer the most important, stable, and independent part of a system.
93113
94114![ #red] ( https://placehold.co/15x15/red/red.svg ) ** Application Layer**
95115
0 commit comments