Post on 22-Jul-2015
About me …
Let's start
DDD?
Domain Driven Design
● Domains, Subdomains, and Bounded Context
● Architecture● Entities● Value Objects● Services● Domain Events● Modules● Aggregates● Factories● Repositories● Integrating Bounded Context● Application
“Value Object: An object that contains attributes but has no conceptual identity. They should be treated as
immutable.”
Wikipedia
“A Value Object is a simple object that is defined by its properties, and reflects a concept from the business domain - e.g. Money, Email Address,
Temperature, Location etc. The main characteristic of a Value Object is that it is
immutable. This means that it has ...”
http://www.phpvalueobjects.info
Numbers
● 42● 3,14● 82● …
Strings
● “Demo”● “@sensorario”● “Domain-Driven Design”● …
Dates
● 10/09/1982● 17/05/2015● …
Currency
● EUR● USD● …
Roadmap
● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources
Characteristics
● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators
Measures, quantifies, or describe
● It is not a thing in your domain● Is a concept that measures, quantifies, or
describe a thing in your domain
Person
● A person has an age● The age measure numbers of year of that
person
Person
● A person has a name● The name is not a thing, but describes how
we can call (the value) the person (the thing)
Characteristics
● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators
Value Object Immutability
Its methods, whether public or private, will not cause its state to change
class FullName {
public static function withNameMiddleSurname(...) {
...
}
public static function withNameAndSurname(...) {
...
}
}
public function testImmutability()
{
$demo = FullName::withNameMiddleSurname(
new StringLiteral("Simone"),
new StringLiteral("Demo"),
new StringLiteral("Gentili")
);
$sensorario = $demo->withMiddleName(
new StringLiteral("Sensorario")
);
$this->assertTrue($sensorario != $demo);
}
class FullName {
private function __construct(...) {
// ...
}
public function withMiddleName(StringLiteral $middleName) {
return new self(
$this->name,
$middleName,
$this->surname
);
}
}
class FullName {
public function withMiddleName(...) {
$that = clone $this;
$that->name = “Davide”;
$that->middleName = “Sbiellone”;
$that->surname = “Bellettini”;
return $that;
}
}
Immutability with references
Value Objects can hold references to Entities, but when referenced Entity change state, Value Object changes too, which violate the quality of immutability
Characteristics
● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Comparability● Side-Effect-Free behavior with collaborators
Conceptual Whole of attributes
A Value Object may possess just one, a few, or a number of individual attributes, each of which is related to the others
Conceptual Whole meaning
Only together do all the attributes form the others, each of the attributes form the complete intended measure or description
Conceptual Whole
● {42 euro} has two attributes● The attribute 42 and the attribute euro● Separately these attributes describe something
else or nothing special
Characteristics
● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Comparability● Side-Effect-Free behavior with collaborators
Replace variable “Value”
$total = 42;
// ...
$total = 4;
Replace variable “Value Object”
Is exactly what replacement even when a given Value Object Type is more complex than an integer
Replaceability
$speaker = Speaker::withFullName(
“Simone”,
“Gentili”
);
$speaker = Speaker::withFullNameAndNickName(
“Simone”,
“Gentili”,
“Demo”
);
Characteristics
● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators
Value Objects Equality
● When tho value objects have same values, they are equals
● Values has no identity
== in PHP
“When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.”
Equality that could differ from ==
public function sameValueAs(ValueObjectInterface $date)
{
if (false === Util::classEquals($this, $date)) {
return false;
}
return $this->getYear()->sameValueAs($date->getYear()) &&
$this->getMonth()->sameValueAs($date->getMonth()) &&
$this->getDay()->sameValueAs($date->getDay());
}
Equality
$simone = new Speaker(
new StringLiteral("Simone"),
new StringLiteral("Gentili"),
new IntegerValue("32")
);
$twin = new Speaker(
new StringLiteral("Simone"),
new StringLiteral("Gentili"),
new IntegerValue("32")
);
$this->assertTrue($simone == $twin);
Characteristics
● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators
Side-Effect-Free Behavior
Since no modification occurs when executing a specific operation, that operation is said to be side-effect-free
Side-Effect-Free Behavior
The methods of an immutable Value Object must all be Side-Effect-Free because they must not violate its immutability quality
Side-Effect-Free Behavior
When a Value's method takes an Entity as parameter, it may be best for it to answer a result that the Entity could use to modify itself on its own terms
Roadmap
● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources
Standard Types Expressed as Values
● Currency (Value)● “EUR, USD, ...” (Standard Types)
Standard Types Expressed as Values
● HTTP Status Code (Value)● “201, 302, 404, ...” (Standard Types)
Standard Types Expressed as Values
Using a Standard Type here helps you avoid bogus currencies (or HTTP Status Codes)
Roadmap
● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources
Persisting Value Objects
When persisted, an instance of a Value Object will occupy its own row in a relational database table that exists specifically for its type, and have its own database primary key column: Collection of Value Objects
Persisting Value Objects
In a general sense, it involves serializing the object to some text or binary format and saving it to disk
Roadmap
● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources
Testing Value Objects
If Values are final classes, cannot be mocked
Roadmap
● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources
Implementations
“If you have achieved the definitive functionality fit your class or method, and you feel that overriding it can only damage the ultimate perfection of your work, you may need the final keyword”
Matt Zandstra
Roadmap
● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources
Github Repositories
● sensorario/kataphp● nicolopignatelli/valueobjects● gws/php-valueobjects● sebastianbergmann/money
Resources
“URIs are values, with identity defined by the value, and thus should be modeled as value
objects.”
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-
message-meta.md#why-are-uris-represented-as-objects
Resources
“A value object should always override .equals() in Java (or = in Smalltalk). (Remember to override .hashCode() as well.)”
http://c2.com/cgi/wiki?ValueObject
Contacts
sensorario@gmail.com
twitter.com/sensorario
instagram.com/sensorario
linkedin.com/in/sensorario
github.com/sensorario
facebook.com/sensorario
Q/A