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.