Skip to content

Domain modeling

Modeling a domain so that the types say which values can exist and which transitions are legal, following Wlaschin’s Domain Modeling Made Functional. Part of Type-driven design.

  • Domain modeling with types: overview of the folder, type-first design, how the vocabulary translates across languages, values, entities and aggregates, and errors as values.
  • Newtypes and simple values: primitive obsession, newtypes, value classes and opaque types, and constrained simple values.
  • Algebraic data types: product and sum types, counting values as a design check, and sum types versus tagged classes and class hierarchies.
  • Making illegal states unrepresentable: business rules as choice types, connascence of value, booleans, a flag plus an optional value, and behaviour as data.
  • Workflows and states: workflows as function types, one type per state, state machines, typestate with phantom types, and long-running workflows.

Explore this section

  • Combining types with AND (product types) and OR (sum types), counting a type's values as a design check, and sum types versus tagged classes and class hierarchies, with Wlaschin, Kurt, Milewski, Parsons, Płachta, Bloch and Ghosh.

  • An overview of modeling a domain with types, following Wlaschin and Kurt: type-first design, how the vocabulary translates across languages, values, entities and aggregates, and errors as values.

  • Designing types so that a value breaking a business rule can't be built: verified emails, \"at least one of\", connascence of value, booleans, a flag plus an optional value, and behaviour as data.

  • Primitive obsession and the newtype pattern, Scala value classes and opaque types, and simple values whose constructors enforce a rule.

  • Modeling a value that moves through states: a workflow as a function type, one type per state, state machines, typestate encodings with phantom types, and long-running workflows.