Skip to content

Commit 5105035

Browse files
author
Fabian Baumeister
committed
fix: Propose anemic domain models
The reason for the anemic domain models lies in two things. First of all are the domain models returned to the adapter. But business logic should not be moved outside. An additional mapping should be avoided. Furthermore in large scaled applications anemic domain models are much easier to handle, because the business logic is not spread across use cases, domain models and services. Making the applicaiton easier to understand
1 parent 1575600 commit 5105035

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
** xref:getting_started/create_rest_service.adoc[]
33
* Architecture
44
** xref:architecture/layered_architecture.adoc[]
5+
** xref:architecture/hexagonale_architecture.adoc[]
56
67
* Integration
78
** xref:integration/rest_service.adoc[]

modules/ROOT/pages/architecture/hexagonale_architecture.adoc

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ image::hexagonal_component_architecture_overview.drawio.svg["devonfw hexagonal a
115115
├── ReservationJpaMapper.java
116116
└── TableJpaMapper.java
117117
----
118-
119-
[cols="1,1", options="header"]
118+
[cols="20,~", options="header"]
120119
|===
121120
| Package | Description
122121

@@ -126,7 +125,7 @@ image::hexagonal_component_architecture_overview.drawio.svg["devonfw hexagonal a
126125
| core.domain
127126
| The domain circle contains the entities and value objects of the business domain of the application.
128127
Related Factories or Builders are located here as well.
129-
The entities in our domain are usually rich.
128+
It's proposed to make entities anemic. See <<_anemic_vs_rich_domain_models>>
130129

131130
| core.usecase
132131
| Use Cases are the main entrypoint of the applications core.
@@ -191,3 +190,29 @@ Inside the adapters further packages are differentiating the protocols that is u
191190
That can be database connections, file operations, API calls, message producing and many more.
192191
Inside the adapters further packages are differentiating the protocols that is used (e.g. `.jpa`).
193192
|===
193+
194+
195+
== Anemic vs Rich domain models
196+
====
197+
"In a rich domain model, as much of the domain logic as possible is implemented within the entities at the core of the application.
198+
The entities provide methods to change state and only allow changes that are valid according to the business rules. [...]
199+
In an “anemic” domain model, the entities themselves are very thin.
200+
They usually only provide fields to hold." <<Hombergs21>>
201+
====
202+
203+
Considering java as an object oriented language it feels natural to implement business logic inside the entities themselves.
204+
In large scale application with mixed skilled people it's proposed to not use rich domain models.
205+
There are two reasons for this.
206+
First of all, the domain objects are returned to the adapters.
207+
If they include business logic this is revealed and available outside of the core, which should not be the case.
208+
The answer to this problem could be an additional mapping, but this leads to a lot of unpractical mappings.
209+
Furthermore, adding the business logic to the domain entities spreads it across use cases, entities and services.
210+
This makes the application more difficult to understand and harder to locate the place for new features or changes.
211+
212+
It's proposed to implement the domain model as anemic entities using Java records.
213+
Use use cases and services to implement the business logic and interact with the domain models.
214+
215+
216+
[bibliography]
217+
== Bibliography
218+
* [[[Hombergs21]]] Tom Hombergs. _Get Your Hands Dirty on Clean Architecture._ 2021.

0 commit comments

Comments
 (0)