liveBook · Manning

Domain layer

As explained in Clean Applications with Hexagonal Architecture

A software layer that contains business concepts, rules, and behavior independently of infrastructure frameworks.

When business rules are scattered across services, controllers, and persistence callbacks, they are difficult to protect from infrastructure changes. The domain layer gathers the rules and invariants that must survive independently of frameworks and deployment choices. section 2.4.1 section 3.1

Keep framework annotations, persistence mechanisms, controllers, messaging, and security infrastructure outside the domain. The inner layer contains pure Java business objects and imports no framework. section 3.1 section 3.10

Place business behavior and concepts in the domain, not merely data:

  • Entities maintain identity and enforce rules on valid state changes; value objects express concepts through their attributes. section 3.1
  • Aggregates and domain services also belong in the core; services handle business logic spanning multiple entities. section 3.1 section 3.10
  • Create domain objects through factories that validate invariants, so invalid objects cannot be instantiated. section 2.4.1 section 3.10

Extracting Article rules

If deleting Spring Boot would break a class, it does not belong in the domain. Article rules that survived that test became the seed of a framework-free domain layer. section 2.4.1

Figure 3.2 The domain layer inside the hexagon. Shows entities, value objects, aggregates, and domain services inside the innermost ring. Ports sit at the ring boundary. Adapters and frameworks are outside. Arrows emphasize that nothing from outside points inward into the domain.

The hexagon diagram locates the domain inside the boundary, with ports at its edge and adapters and frameworks outside. section 3.1

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Specifies domain building blocks and factory-based invariant validation, including collecting multiple errors with Vavr Validation.
Examples
Uses Article rules, a constrained Title value object, and factory methods to illustrate domain behavior and construction.
What you can do after
Build framework-free domain entities whose factories validate invariants before creation.
How it connects
Connects the domain layer to Domain-Driven Design, entities, value objects, aggregates, domain services, ports, and adapters.

Also explained in

Compare how 5 books explain domain layer →

Related concepts

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