In the Part 2 and Part 3 of the Domain Model Structure series, we continued our discussion around how to organize classes inside your domain model. We defined Entities, looked at their relationships, and reviewed options for loading them into memory. This month, we will take a look at Value Objects.
Discovering Value Objects
Value Objects are domain model elements with no conceptual identity. They represent descriptive aspects of the domain and are fully defined by their state. Their behavior depends on what they are, not who they are. If they need to be tracked, they should be tracked with the elements they describe. Thus, they do not need an identity of their own.
We prefer to keep Value Objects immutable, i.e. changeable by full replacement only. Immutable Value Objects are safe to share (which increases performance), and easy to understand (which allows you to build complex behaviors). Let me give you an example based on a classical OO problem:
Assume you have two buckets of paint: b1 and b2. The first bucket b1 contains 1 gallon of yellow paint. The second bucket b2 contains 1 gallon of blue paint. Without looking into the code, could you answer the following questions about the results of the operation b1.Add(b2)?:
- What will be returned by the Add method? Is it 2 gallons of green paint or nothing?
- What will be in the the first bucket b1? Is it 2 gallons of green paint or 1 gallon of yellow paint?
- What will be in the second bucket b2? Is it 1 gallon of blue paint or nothing?
If you notice, we do not have a requirement to track buckets of paint. Thus, I will assume that Bucket is a Value Object and should be immutable. Since both buckets are immutable, they will contain their original values of 1 gallon of paint each and the result of the Add operation will be 2 gallons of green. With no convention in place, it would be impossible to answer these questions without checking the code or better yet unit tests first.
You domain has many Value Objects, a lot more than Entities. If you are having troubles seeing them in your system, take a closer look at the Entities. How are they being described? What are their attributes and properties? How are they being searched for or ordered by? The chances are you will be talking about Value Objects.
Examples: Address, DayPoint, Money, Range, SSN, etc.
Domain Model Structure
Value Objects along with Entities are main elements of the domain model. Many of them are small simple classes that will be widely used in your application.
Place them together in a sub-folder under the Domain Model root. We name this folder "Capability":
You are likely to have a number of very complex Value Objects as well. I will try to discuss some of them in my future articles.
Summary and Additional Tips
- Never define an identity for a Value Object.
- Consider overriding equality operators to match Value Objects using object attributes rather than object references.
- Keep Value Objects immutable.
- Place Value Objects used across many applications into a dedicated Domain Kernel library.
- If you are interested in creating a simple base Value Object class, you can start with the one listed below:
Happy coding! To be continued...