liveBook · Manning

Chapter 6: Implementing the Domain Model

What you will learn in chapter 6 of Data-Oriented Programming in Java

The chapter introduction explains why implementing the invoicing domain model requires care with Java methods and decisions about code organization.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why implementing the invoicing domain model requires care with Java methods and decisions about code organization.

Key terms: data model, methods, side-effects, class ownership, data, architectural choices

6.1 On the criteria by which we break down an implementation

Separating how data is obtained from what is done with it makes the behavior deterministic, providing a criterion for organizing the implementation.

Key terms: deterministic, how we get data, what we do with it

6.1.1 Functions, Purity, and Determinism

This section distinguishes pure, deterministic mathematical functions from other Java methods and explains why that distinction helps organize code that is easier to reason about and test.

Key terms: mathematical functions, deterministic, immutable instance state, purity, pure, impure, side-effects, determinism, dependencies

6.1.2 How do we write pure functions in Java?

This subsection presents static methods as a Java convention for signaling deterministic functions while acknowledging that static does not enforce purity.

Key terms: purity, deterministic functions, static methods, instance state

6.1.3 The effect of deterministic functions on reasoning

This section explains how deterministic functions narrow the behaviors a function can express, making reasoning, testing, and composition easier while leaving interactions with the outside world separate.

Key terms: pure functions, deterministic functions, impure functions, inputs, outputs, combine functions, outside world, non-deterministic, deterministic, how you get data

6.1.4 Functions let you work locally

The section explains how functions that take all needed information as arguments make code and tests easier to understand locally.

Key terms: mathematical functions, arguments, output, local reasoning, test setup, inputs, outputs

6.1.5 Functions are tables of immutable data in disguise

The section explains why a deterministic function can be viewed as immutable input-output data and why that equivalence is useful in testing and debugging.

Key terms: determinism, lookup table, inputs and outputs, immutable data, pre-determined answer, secret state updates

6.1.6 Functions let us model relationships

The section models grace period as a date-to-date relationship selected by customer rating, rather than as a fixed number of days, and explains why naming that relationship matters.

Key terms: grace period, input date, output date, fixed offset, CustomerRating, deterministic function, function type, gracePeriod, domain concept, domain language

6.1.7 One last note on all this deterministic business

This subsection explains why a data-oriented Java design can use practically deterministic code even when some data depends on mutable identity objects.

Key terms: basically deterministic, actually deterministic, identity objects, data, mutable reference

6.2 Organizing code around determinism

The section uses determinism to divide the late-fee pipeline into a deterministic core and a non-deterministic outer layer, with dependencies flowing toward the core.

Key terms: deterministic, service calls, side effects, deterministic core, non-determinism, side-effects, functional core; imperative shell

6.3 Implementing the deterministic core

This section explains why the deterministic business logic should have a consistent, recognizable place in the code, while leaving its exact organization flexible.

Key terms: deterministic core, Core, deterministic business logic

6.3.1 Implementing collectPastDue

The section shows how the domain model narrows the implementation of collectPastDue, then explains why exhaustive case statements are safer than a map for grace periods.

Key terms: type signature, data types, filtering logic, gracePeriod, map, Rating enums, case statements, exhaustiveness, compile time guarantees

6.3.2 Implementing BuildDraft

This section implements draft late-fee calculations and exposes a currency assumption that the domain model does not enforce.

Key terms: draft, types, due dates, Net 60, line items, fee, percentage, functions, USD, original modeling

6.3.3 That really gross wrong feeling is a feature

An invoice’s USD type exposes a gap in where the application enforces that invariant, and the section argues for defending against the gap without fixing the whole codebase at once.

Key terms: USD data type, invariant, type system, friction, front door

6.3.4 Implementing Assess Draft

This section explains how draft assessment uses the draft total to decide whether to bill and why the assessment must account for an approval that may not exist.

Key terms: draft, allowed range, Approval, created lazily, Optional

6.3.5 Dealing with optional

This section argues that Optional code should be chosen for what it communicates to readers, not for allegiance to an imperative or functional style.

Key terms: Optional, Approval, imperative, functional, orElse

6.3.6 It’s always OK to introduce more types!

This section shows how naming each assessment case with a type can clarify a domain decision that an if/else chain leaves implicit.

Key terms: Assessment, else, types, main function, refactorings

6.4 Implementing the non-deterministic shell

This section explains how to connect a deterministic core to non-deterministic work while keeping the surrounding methods predictable.

Key terms: non-deterministic work, deterministic core, sandwich, type signature, functions, pattern match

6.4.1 Who owns the save logic?

This subsection chooses a practical way to save a LateFee without making the existing persistence system understand the new domain type.

Key terms: LateFee, Invoice, data-oriented, save

6.4.2 Treat legacy Entities as Data Transfer Objects

This subsection shows how to connect a new domain model to legacy persistence by treating the existing Invoice Entity as a data transfer object.

Key terms: Invoice Entity, Data Transfer Object, deterministically map, semantic domain type, generic Invoice Entity, existing machinery, repositories

6.5 Data is the ultimate interface

The section explains how well-formed data and deterministic logic make it easier to change an application's architecture when a sequential for-loop is no longer sufficient.

Key terms: data types, seams, natural interfaces, deterministic

6.6 Putting it all together

6.7 Wrapping up

The section identifies determinism as the guiding idea behind implementing the Late Fees data model.

Key terms: determinism, Late Fees data model

6.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Modelling Domain Behaviors Chapter 7: Reasoning About Correctness →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.