Chapter 3. Designing functional domain models

 

This chapter covers

  • Designing domain models—the functional and algebraic way
  • Decoupling the algebra of the domain from its implementation
  • Enforcing the laws of algebra in designing APIs
  • Implementing lifecycle patterns of domain objects

Previous chapters covered the parallels between functional programming and mathematics in general, and algebra in particular. You explored algebraic data types, sum types, and product types, and you learned how to combine them to form abstractions that model your domain elements. This chapter takes this discussion to the next level; you’ll begin with specifications of the model and, using algebraic composition of types, build APIs for our domain model. These APIs are contracts that obey the laws of the domain, and you’ll use the algebra of types to ensure that they’re correctly constructed. You’ll learn how to develop APIs as algebraic specifications and write properties that verify the laws that form the business rules.

You’ve already seen the three basic lifecycle patterns of domain objects: aggregates, factories, and repositories. This chapter shows how to implement them in your domain model by using the basic idioms of functional programming.

Figure 3.1 illustrates how you’ll progress through the various sections and increase your knowledge of using functional techniques to build domain models.

Figure 3.1. The progression of this chapter’s sections

3.1. The algebra of API design

3.1.1. Why an algebraic approach?

3.2. Defining an algebra for a domain service

3.2.1. Abstracting over evaluation

3.2.2. Composing abstractions

3.2.3. The final algebra of types

3.2.4. Laws of the algebra

3.2.5. The interpreter for the algebra

3.3. Patterns in the lifecycle of a domain model

3.3.1. Factories—where objects come from