3 Building the domain core
In this chapter, the domain core is built from the inside out, with the focus on business concepts that must always remain correct no matter what frameworks or infrastructure surround them. The main idea is that the domain layer should contain only business rules and invariants, expressed through plain Java objects that are self-validating, framework-free, and easy to test. Rather than starting with adapters, persistence, or APIs, the chapter argues for beginning with the concepts the business cannot afford to get wrong and modeling them so invalid states cannot exist.
The chapter develops this approach through the Always-Valid Domain Model: domain objects are created only through factory methods that validate all rules before construction, and the constructor itself remains private. This avoids the dangers of creating invalid objects and pushing validation into application services or calling code. It also introduces value objects as a way to replace primitive obsession with explicit, type-safe concepts such as titles, IDs, and publication periods, making constraints local, reusable, and impossible to ignore. Domain services are presented for logic that spans multiple entities or aggregates, while factories are used to coordinate more complex creation cases without leaking business rules into outer layers.
Finally, the chapter separates business invariant validation from input validation, showing that each belongs at a different boundary. Input validation handles syntax and shape at the edges of the system, while semantic validation belongs in the domain and protects the integrity of the model. The result is a design where creation is explicit, errors are handled as part of normal control flow, and the domain can be tested in isolation with minimal setup. The chapter closes by reinforcing practical techniques such as guard clauses and by emphasizing that clean domain design is what makes the rest of the architecture simple, robust, and adaptable.
The contrast between domain and infrastructure concerns.
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.
Persistence and serialization frameworks may require construction paths that do not express domain rules clearly. The domain model keeps creation explicit: callers enter through validateThenCreate, and the private constructor is reached only after invariants pass
Primitive obsession vs value objects. Left: method signature with String, String, Long parameters—no type safety, validation scattered. Right: same method with Title, Content, AuthorId parameters—each type self-validating, compiler enforces correctness.
Validation checkpoints across layers. Shows the flow from HTTP request through adapter → command → handler → domain entity → repository. Green checkmarks at adapter (transport validation), command (input validation), and domain entity (business invariants). Gray pass-through at the handler and repository—they trust upstream validation.
Summary
- The domain layer is the innermost ring of the hexagon. It contains entities, value objects, aggregates, and domain services—all pure code with no framework dependencies.
- Three functional principles—purity, encapsulation, and completeness—guide domain design. Maximize them where they matter most: in entities and value objects.
- The Always-Valid Domain Model is the single most important design decision in the domain layer. The constructor is private; the only way to create a domain object is through a factory method that validates all invariants first. If validation fails, the factory returns an error, not an invalid object.
- The recommended pattern is validateThenCreate using Vavr's Validation type, which can run several checks and return all validation errors together instead of stopping at the first one. The CanBeCreated pattern is a defensible alternative when CQS adherence is a priority.
- Value objects replace simple language types with domain concepts. A Title type that enforces its own constraints is safer and more expressive than a raw String.
- Domain services handle business logic that spans multiple entities. They live in the domain layer, carry no framework annotations, and are pure functions of their inputs.
- Factories encapsulate object construction. For simple entities, the factory method lives on the entity class. For complex construction involving identity generation or external lookups, use a standalone factory class.
- Input validation (syntactic) guards the hexagon's boundaries. Business invariant validation (semantic) guards the domain itself. They use different mechanisms and live in different layers.
FAQ
What belongs in the domain layer of a hexagonal architecture?
The domain layer should contain only business concepts that represent rules or invariants the system must always enforce, regardless of framework or deployment. Entities, value objects, aggregates, and domain services belong there. Persistence code, REST controllers, messaging, security infrastructure, and framework annotations do not.Why should domain objects be always valid?
Because invalid domain objects should never exist in memory, even briefly. The Always-Valid Domain Model eliminates bugs by ensuring that an object is only created if all business invariants are satisfied. If creation fails, the factory returns an error instead of an invalid object.Why use a factory method instead of a public constructor for entities?
A private constructor plus a validating factory method makes object creation explicit and safe. The factory checks all invariants before construction, so callers cannot accidentally create invalid entities. This also keeps validation inside the domain, rather than spreading it across handlers or other layers.What is the recommended pattern for creating a valid domain entity?
The chapter recommends avalidateThenCreate factory method. It validates all required inputs, combines the results, and returns either a valid entity or a collection of domain errors. This keeps creation and validation in one place and makes failure part of the normal return type.What is the difference between Validation and Either?
Validation is used when you want to collect multiple errors, such as validating several fields at once. Either is better for sequential operations where each step depends on the previous one succeeding. In the chapter, Validation is used for construction-time checks, while Either is used for orchestration in handlers.Why are value objects important in the domain core?
Value objects make implicit concepts explicit. Instead of passing raw primitives likeString or Long, the code uses types such as Title, Content, and AuthorId. This removes primitive obsession, centralizes validation, and makes method signatures self-documenting.
Clean Applications with Hexagonal Architecture ebook for free