My Journey in Learning Domain-Driven-Design part4 (Value-objects)

Ahmed Ibrahim
4 min readJan 25, 2021

In part1, we had an introduction about Domain-Driven-Design Tactical modeling. In this part, we will take a deep dive into one of the Tactical Modeling called value-objects.

we will know what are value objects?. what are value objects characteristics?. what are the benefits of value objects?. with code examples using Typescript.

What are value objects:

value objects are objects that identify values. and has no unique identity. Value types that measure, quantify, or describe things are easier to create, test, use, optimize, and maintain.

an example of values we can model it as value objects: person’s name full name composed of first name and last name, phone number, email, passwords.

Value objects characteristics:

1- Measures, Quantifies, or Describes:

Value Object is actually a concept that measure, quantifies, or otherwise describes a thing in the domain. A person has an age. Age is not really a thing but measures or quantifies the number of years the person (thing) has lived. A person has a name. The name is not a thing but describes what the person (thing) is called.

2- Immutable:

Once the object has been created we can’t change its state, if we want to we need to replace it by creating a new object with the new state.

make sure when creating a value object that you remove setters or make it private that it can’t be accessible from outside.

Special Cases when can use mutable value objects

  • If the VALUE changes frequently
  • If object creation or deletion is expensive
  • If replacement (rather than modification) will disturb clustering
  • If there is not much sharing of VALUES, or if such sharing is forgone to improve clustering or for some other technical reason

Please see Eric Evans book page [84] for more information about it

3- Conceptual Whole:

A Value Object may possess just one, a few, or a number of individual attributes, each of which is related to the others. Each attribute contributes an
important part to a whole that collectively the attributes described. Taken apart from the others, each of the attributes fails to provide a cohesive meaning. Only together do all the attributes form the complete intended measure or description.

for example, a person’s full name consists of first name and last name :

FullName Value object with primitive type

However, some people say that we have to limit the usage of primitive types as a string so the value object of FullName maybe like this:

FullName value object without primitive types

with creating a type for first name and last name. maybe the client of that value object wants to fix a capitalization issue with the name. so we can centralize all of that code in FirstName and LastName classes.

4- Value Equality:

When a Value Object instance is compared to another instance, a test of object
equality is employed. Throughout the system there may be many, many Value
instances that are equal, and yet not the same objects. Equality is determined
by comparing the types of both objects and then their attributes. If both the
types and their attributes are equal.

you could assign (using replacement) any one of the equal Value instances to an Entity’s property of that type and the assignment would not change the value of the property.

Here’s an example of class FullName implementing a test for Value
equality:

5- Side-Effect-Free Behavior:

A method of an object can be designed as a Side-Effect-Free Function [Evans].
A function is an operation of an object that produces output but without modifying its own state. The methods of an immutable Value Object must all be Side-Effect-Free Functions because they must not violate its immutability quality.

benefits of value objects:

1- Readability:

Imagine we have a list of phone numbers we would write it like this

const PhoneNumbers: String[] = [...phoneNumbersStrings];

But with value objects would be like this:

const phoneNumbers: PhoneNumbers[] = [...phoneNumbersValueObjects];

2- self-validation:

As value objects are immutable. there can’t be an invalid value object as it encapsulates its validation. and once it exists it can’t be changed.

password value object example:

As we can see. there won’t be any invalid password value object that exists.

3- Reusability & Performance:

We can reuse value objects if they are equal review flyweight pattern.

Conclusion:

Value objects are important for Domain-Driven-Design. and I think we can use them outside Domain-Driven-Design too.

References:

Also, Check my My Journey in Learning Domain-Driven-Design previous parts

Part1(Intro To domain Driven Design)

Part2(Layers and Hexagonal Architecture)

part3(CQRS)

--

--