9 Building rich domains

 

This chapter covers

  • Working with missing values
  • Working with errors
  • Computation expressions
  • A worked example for modeling domains in F#

In the previous chapter, we more or less finished the language part of the book. There will still be a few new features scattered around, but we’ll start to look at applying those techniques in different scenarios now and looking at F# libraries (both out-of-the-box and third-party) that you should know.

This chapter builds on the previous one, which discussed discriminated unions, by focusing on two specific types that come bundled with F#: Option and Result. These are two key types that are bundled with F# and are very important to learn in order to model domains correctly and effectively.

9.1 Working with missing values

Missing data is a natural part of working with not only data from third parties (where we might not be in control of the data and quality may be sketchy) but simply a part of modeling in the real world. We deal with this every day. For example, when registering with an e-commerce website, your account will always have an email address, and it might have your home address. Maybe you haven’t gotten around to providing your home address or perhaps you never intend to because your only purchases will be digital ones.

9.1.1 The billion-dollar mistake: Working with nulls

9.1.2 Options are the answer

9.1.3 Option.map

9.1.4 Option.bind

9.1.5 Interop with .NET

9.1.6 Options and collections

9.2 Handling errors

9.2.1 Results

9.2.2 Handling multiple errors

9.2.3 Strongly typed errors

9.2.4 Exceptions

9.2.5 When to safely leave options and results

9.2.6 When to use options, results, and exceptions

Summary