liveBook · Manning

Anemic domain model

As explained in Grokking Software Architecture

An anemic domain model is a domain model whose objects contain little or no domain behavior, leaving rules in other components.

An anemic domain model is a collection of data objects with little or no business behavior. Rules that belong with the data are scattered elsewhere, often in a fat controller. The result: objects cannot protect their own state, and behavior is less cohesive. section 4.7.2

Listing 4.8 Before (The Symptom)
class Order { #A
    constructor() {
        this.id = 0;
        this.total = 0.0; #B
        this.customerEmail = ""; #B
        this.items = []; #B
    }
}

The Order class stores an ID, total, customer email, and items, but has no behavior to validate or protect them; another class could set its total to -100. section 4.7.2

Keep core business rules with the domain model so they can be tested in isolation. Put orchestration in the application layer, HTTP concerns in presentation, and database or email concerns in infrastructure. A rich domain model plus automated architectural checks helps keep those boundaries from eroding. section 4.7.4

How Grokking Software Architecture covers it

How deep it goes
Explains the data-only Order example and its encapsulation and cohesion problems, then contrasts it with a rich model without showing its implementation.
Examples
Uses an Order object with data fields but no behavior, including the risk of assigning it a negative total.
What you can do after
Apply architectural boundaries by placing business rules in the domain model and verifying layer restrictions with automated tests.
How it connects
Pairs the anemic domain model with the fat controller and contrasts it with the rich domain model and encapsulation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.