Contents
Can a value object be an aggregate?
An aggregate is a collection of one or more related entities (and possibly value objects). Each aggregate has a single root entity, referred to as the aggregate root. Thus, the aggregate root must be an entity, not a value object, so that it can be persisted to and from a data store using its ID.
What is a value object and why should you use them?
Value objects represent typed values, that have no conceptual identity, in your domain. They can help you write better code that is less error-prone, more performant and more expressive.
Is address a value object?
Address should almost always be a value object, since in most domains, the address is indeed just a value. The fact that a Company and a Person have the same Address today does not mean that if one changes, the other should change too.
The aggregate supports concepts at the business level and the boundary of the aggregate is what enforces invariants, e.g. business rules like maximum order amount. Persisting and retrieving always work through the aggregate root and always as a whole aggregate. Value objects are for Rayner a key building block pattern, some reasons being:
When to move behaviour away from aggregates to value objects?
When modelling Aggregates move as much as possible of the behaviour away from the Entities within the aggregate into Value Objects, As more behaviour is needed this is added as new value objects, Paul Rayner recommends in a series of blog posts covering aggregates, entities and value objects, all concepts from Domain-Driven Design (DDD).
What’s the difference between an agg and an entity?
I’ve always had problems with Aggregates vs. Entities. Is it that Agg’s are transactional boundaries that expose behaviors (methods) that the entities they contain may perform? Entity is a business concept that exposes behavior. A collection of entities may have different behavior varied upon the type of aggregate that encapsulates it?
How are aggregates and entities used in Domain Driven Design?
A Purchase Order would probably need to have at least one Line Item to be valid. Here is a case of two or more objects that seem to belong together most of the time in terms of how you need to work with them. So treat PO as an aggregate of the PO entiity and the Line Item value objects. And make the PO entity the root of the aggregate.