In this chapter, we started to write a lot of code and learned the basics of implementing domain models in code. We looked at entities and value objects, what they are needed for, and how different they are. Explaining the power of value objects consumed a significant part of this chapter, but this topic is vital since value objects are often overlooked.
We used factory functions to create different ways of constructing value objects. A similar technique can be used to form valid entities, but we were not touching this topic just yet. We also used a domain service to make use of some external services inside our value object, while keeping the domain model itself clean from any external dependencies.
Constraints and invariants that play such an important role in keeping the state of the system valid at all times were also discussed, and we used different techniques to...