Skip to content
Ameen Abozaid
MID-060

DDD introduction

  • 7 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Entities
  • value objects
  • aggregates
  • repositories
  • domain services

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Distinguish entities and value objects and model each in Java.
  2. Design aggregates around invariants and keep them small, referencing other aggregates by ID.
  3. Use repositories per aggregate and domain services for logic that spans aggregates.
  4. Build a ubiquitous language with domain experts and reflect it in code.

Lessons

  1. 01Why domain-driven design
  2. 02Ubiquitous language
  3. 03Entities and value objects
  4. 04Aggregates and invariants
  5. 05Repositories and domain services
  6. 06Domain events
  7. 07Demo: redesigning the Order aggregate

Lab and exercises

Lab

MID-060-LAB1 · Order aggregate

Outcomes: 1–3 · Time: ~3 h

  1. Redesign Order with OrderId, Money, OrderLine; remove public setters.
  2. Enforce inside Order: status transitions (NEW → PLACED → PAID → SHIPPED), line totals, and cancellation rules (no cancel after shipping).
  3. Raise OrderPlaced and OrderCancelled events from the aggregate.
  4. Keep OrderRepository as a domain interface; services only orchestrate.
  5. Write unit tests per invariant; add a short glossary of the language used.

Acceptance: no invariant enforced in services; unit tests per invariant.

Exercises (assessment: aggregate design for an unseen domain; six exercises)

IDOutcomeTask
MID-060-EX011Classify as entity or value object: Customer, Money, Address, Order, DateRange. Justify each.
MID-060-EX021Implement Email as a record that rejects invalid input, plus an equality test.
MID-060-EX032For a hotel booking domain, list the invariants and draw the aggregates; keep them small.
MID-060-EX042Find and fix the problem: class Order { Customer customer; List<Product> products; }.
MID-060-EX053Decide where logic goes: "apply a voucher across several orders", "an order cannot ship unpaid".
MID-060-EX064Run a mini glossary session on a domain of your choice (6 terms) and rename five code identifiers to match.
Show solutions
  • EX01 Entities: Customer, Order (identity). Value objects: Money, Address, DateRange (equal values interchangeable).
  • EX02 record Email(String value) { Email { if (!value.matches(".+@.+\\..+")) throw new IllegalArgumentException(); } }; two instances with the same value are equal.
  • EX03 Example: Booking protects "check-out is after check-in" and "status flow"; "no overlapping bookings for a room" is a cross-aggregate rule, handled by a Room availability aggregate or a domain service. Keep Booking, Room, Guest separate, linked by IDs. Accept any design that ties aggregates to invariants.
  • EX04 Replace with CustomerId and List<OrderLine> holding ProductId, quantity, and unit price snapshot; aggregates reference others by ID.
  • EX05 Voucher across orders: domain service (spans aggregates). "Cannot ship unpaid": inside Order.ship().
  • EX06 Judged against the glossary; code names must match business terms.

Quiz

Outcomes: O1 entities and value objects · O2 aggregates · O3 repositories and domain services · O4 ubiquitous language.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Which has an identity that persists as attributes change?O1
  2. 2Value objects should be:O1
  3. 3An invariant is:O2
  4. 4An aggregate should reference another aggregate by:O2
  5. 5One transaction should normally change:O2
  6. 6The best sign of an aggregate that is too big:O2
  7. 7A repository is provided for:O3
  8. 8Logic spanning several aggregates belongs in:O3
  9. 9The ubiquitous language is:O4
  10. 10An anemic domain model means:O2

Practical part

Six exercises MID-060-EX01 to EX06 plus the lab. Pass: at least five exercises correct, and the lab's Order enforcing every invariant with a unit test each.

Common mistakes

One giant aggregate; anemic models with setter-based "domain"; aggregates holding object references to other aggregates.

Assessment

Aggregate design for an unseen domain.

Career relevance

Strategic DDD (SRA-003).

Type to search levels, phases, modules and lessons.