BT

Your opinion matters! Please fill in the InfoQ Survey!

Aggregates, Entities and Value Objects in Domain-Driven Design

| by Jan Stenberg Follow 9 Followers on Jan 31, 2015. Estimated reading time: 2 minutes |

A note to our readers: As per your request we have developed a set of features that allow you to reduce the noise, while not losing sight of anything that is important. Get email and web notifications by choosing the topics you are interested in.

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).

One common example describing aggregates is using a Purchase order with Line items connected to the order. Two cohesive objects like this is a good example of an aggregate and since in practice a business always works in the context of the order, never with line items individually, the purchase order becomes the root of the aggregate. 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:

  • Adds expressive names to key concepts in a model.
  • Increases the conceptual cohesiveness of objects.
  • Enables moving from using primitives for simple domain concepts like name of a customer.
  • Allows entities to focus on identity by delegating behavioral logic to value objects.

Rayner emphasizes that although Data Transfer Objects (DTOs) superficially looks like value objects they are just a technical solution predominantly used to transfer data, e.g. between layers. Value objects on the other hand represent concepts in the business domain.

Rayner recommends that value objects should be immutable and quotes Joshua Bloch:

Classes should be immutable unless there’s a very good reason to make them mutable….If a class cannot be made immutable, limit its mutability as much as possible.

Other reasons Rayner sees for making value objects immutable are:

  • When used as keys or identifiers since these must not change.
  • They are inherently thread-safe, simplifying working with concurrency.

Techniques for making objects immutable varies with programming languages and Chris Oldwood, a freelance developer, has described some examples of doing this.

When persisting value objects to a relational database (RDBMS) Rayner describes a few options:

  • Inline value objects fields in the entity table, a simple design that also supports refactoring of value objects from an entity.
  • Using a table for each type of value object, a design that allows for a list of value objects.
  • Serializing the value object and store in a field in the entity table is a method that Rayner recommends for lists. Some databases support plain text formats like JSON but querying and indexing may have limited support. There may also be issues if the result of a serialization is huge.

Using a document database or a combination where one part of an object is stored in a RDBMS and the rest in NoSQL storage, cross-store persistence, are other options. Many document databases of today supports querying the documents without a need to store searchable properties separately.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT