liveBook · Manning

Chapter 5: Modelling Domain Behaviors

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

Introduces the chapter’s plan to apply data-oriented modeling to a complex feature within an existing application.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s plan to apply data-oriented modeling to a complex feature within an existing application.

5.1 "Encouraging timely payments"

Introduces the late-fee feature, its billing and audit requirements, and the existing system in which it must operate.

Key terms: past due, outstanding balances, late fees, manual approval, auditable, persisted, existing application, ORM, service integrations

5.1.1 Invoicing (briefly)

This section explains what an invoice represents, when it is considered late, and why the service cannot finalize it on its own.

Key terms: invoice date, due date, payment terms, customer's rating, grace period, invoice, open, closed, invoice numbers, central billing service

5.1.2 The Legacy System!

This section establishes the fragmented services and pre-existing systems that the invoicing feature must work with, while simplifying how the services behave.

Key terms: web services, blocking and synchronous, entities, ORMs, prior decisions

5.1.3 A bunch of prior decisions designed around “free"

The section establishes an entity-centered legacy application whose ORM-friendly modeling choices shape where the new feature must fit.

Key terms: identity objects, Entity, ORM, Late Fee Invoice, normal Invoice, ORMs, enums, nullable fields, canonical representations, Repository

5.2 Modeling domain behaviors around existing code

This section shows how assembling a feature from existing services and entities can hide its business purpose and permit dangerous misuse, then argues for modeling domain behaviors as deliberately as data.

Key terms: services, entities, requirements, conscious design, Invoice, Late Fee invoices, runtime defenses, business rule, modeling behaviors, modeling data

5.3 Modeling domain behaviors around data

The section presents domain behavior as transformations between data states and keeps the initial model separate from implementation details.

Key terms: behaviors, states, inputs, outputs, machine, raw Invoices, Late Fees, pairs of data, data types, implementation details

5.3.1 Telling a story with data

The section uses a sequence of data types to tell the late-fee story, making past-due invoices, draft invoices, and decisions about drafts visible before implementation.

Key terms: Invoice, LateFee, past due invoices, transformations between data types, Draft, Open, billing system, lifecycle, LateFeeInvoices, billable

5.3.2 Represent runtime decisions as compile time algebraic data types

The section shows how representing decisions as data makes domain branches visible and helps sketch behavior as a sequence of data transformations.

Key terms: if/else, algebraically, decision itself as data, interpreters, Drafts, LateFeeInvoice, life cycle states, billed, not billed, invented inputs

5.4 Fleshing out the representation

This section explains the tradeoffs involved in turning a sketch of domain data into usable types that support the intended behaviors.

Key terms: semantics, behaviors, representation, bad states, ergonomics, clear semantics, iterative, tradeoffs

5.4.1 Creating wrapper types and being OK with comingled Identities

The section uses a PastDue record to make a fact about an Invoice explicit while accepting the mutable identity inside the wrapper.

Key terms: PastDue, Invoice, simple wrappers, records, mutable identity, value, clarity, shallow immutability, quarantine

5.4.2 Being OK with imperfect semantics

This section explains why a PastDue wrapper can be useful even though it cannot enforce its full meaning, and how to weigh that imperfection in a data-oriented design.

Key terms: PastDue, NonNegativeInt, Degree, semantics, wrapper type, clarity of communication, semantic imperfections

5.4.3 Designing the algebra for Late Fees

This section compares ways to model a late fee’s lifecycle in Java and uses generics to balance usable data types with compile-time lifecycle constraints.

Key terms: Draft, ApprovalStarted, Billed, Rejected, semantic invariants, wrappers, sealed, LateFee, pattern matching, lifecycle

5.4.4 Modeling the decisions

The section models decisions about draft late fees as data, uses lifecycle types to restrict assessments to drafts, and explains why designing before implementation helps explore alternatives.

Key terms: decisions, data types, attributes, draft late fees, LateFee, Assessments, Drafts, Billed, Rejected, type system

5.4.5 The high-level data model

The section explains why a multi-type data model can be worth its extra code: it makes the domain understandable and constrains billing at compile time, while leaving room to choose less typing.

Key terms: data-oriented code, LateFee, Lifecycle, review type, safe by construction, type safety, invariants, amount of typing

5.5 Interacting with legacy code

Introduces three ways to connect a newly designed data model to legacy code and explains the tradeoff between greater control over semantics and the cost of changing existing code.

Key terms: what’s already there, how, what, external concerns, data-oriented, semantics, deeper cuts into existing code

5.5.1 Fitting into what’s already there

This subsection shows how fitting a domain design directly around existing dependencies can obscure its purpose and make its behavior harder to trust.

Key terms: buildDraft, external dependencies, existing services

5.5.2 Separate how you get the data from what you do with it

This section separates fetching data from using it, explains why that boundary helps, and identifies a remaining coupling to external service layouts.

Key terms: external dependency, core, how, what, atomicity, side-effects, network calls, database reads, buildDraft, assessDraft

5.6 Decoupling from the Tyranny of What's Already There

This section shows how a data-access boundary can assemble the data a feature needs without exposing the services and repositories that supply it.

Key terms: smart access layer, boundary layer, plain data, Customer, Payment Terms, Ratings, Approvals, façade, repository, Data Access Object

5.7 The finished data model

The section explains why a purpose-built data model is worth considering despite its visible mapping and maintenance costs.

Key terms: plain data, identity objects, representations, mapping code, boundaries, domain, legacy code, requirements, new data types

5.8 Wrapping up

The section reflects on how focusing on data makes messy real-world requirements visible and gives developers control over enforcing invariants.

Key terms: data-oriented programming, requirements, ambiguity, algebra of our data, invariants

5.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Domain Modeling with Algebraic Data Types Chapter 6: Implementing the Domain Model →

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.