How to Translate Domain Types to DTOs

The domain types that we define can be quite complex, yet the corresponding DTO types must be simple structures containing only primitive types. How then do we design a DTO given a particular domain type? Let’s look at some guidelines.

Single-Case Unions

Single-case unions—what we are calling “simple types” in this book—can be represented by the underlying primitive in the DTO.

For example, if ProductCode is this domain type:

 type​ ProductCode = ProductCode ​of​ ​string

Then the corresponding DTO type is just string.

Options

For options, we can replace the None case with null. If the option wraps a reference type, we don’t need to do anything because null is a valid value. For value types like int, we’ll ...

Get Domain Modeling Made Functional now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.