+ All Categories
Home > Technology > #pugMi - DDD - Value objects

#pugMi - DDD - Value objects

Date post: 22-Jul-2015
Category:
Upload: simone-gentili
View: 85 times
Download: 0 times
Share this document with a friend
67
Transcript
Page 1: #pugMi - DDD - Value objects
Page 2: #pugMi - DDD - Value objects

About me …

Page 3: #pugMi - DDD - Value objects
Page 4: #pugMi - DDD - Value objects
Page 5: #pugMi - DDD - Value objects
Page 6: #pugMi - DDD - Value objects
Page 7: #pugMi - DDD - Value objects

Let's start

Page 8: #pugMi - DDD - Value objects

DDD?

Page 9: #pugMi - DDD - Value objects
Page 10: #pugMi - DDD - Value objects
Page 11: #pugMi - DDD - Value objects

Domain Driven Design

● Domains, Subdomains, and Bounded Context

● Architecture● Entities● Value Objects● Services● Domain Events● Modules● Aggregates● Factories● Repositories● Integrating Bounded Context● Application

Page 12: #pugMi - DDD - Value objects
Page 13: #pugMi - DDD - Value objects

“Value Object: An object that contains attributes but has no conceptual identity. They should be treated as

immutable.”

Wikipedia

Page 14: #pugMi - DDD - Value objects

“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

Page 15: #pugMi - DDD - Value objects
Page 16: #pugMi - DDD - Value objects

Numbers

● 42● 3,14● 82● …

Page 17: #pugMi - DDD - Value objects

Strings

● “Demo”● “@sensorario”● “Domain-Driven Design”● …

Page 18: #pugMi - DDD - Value objects

Dates

● 10/09/1982● 17/05/2015● …

Page 19: #pugMi - DDD - Value objects

Currency

● EUR● USD● …

Page 20: #pugMi - DDD - Value objects

Roadmap

● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources

Page 21: #pugMi - DDD - Value objects

Characteristics

● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators

Page 22: #pugMi - DDD - Value objects

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

Page 23: #pugMi - DDD - Value objects

Person

● A person has an age● The age measure numbers of year of that

person

Page 24: #pugMi - DDD - Value objects

Person

● A person has a name● The name is not a thing, but describes how

we can call (the value) the person (the thing)

Page 25: #pugMi - DDD - Value objects

Characteristics

● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators

Page 26: #pugMi - DDD - Value objects

Value Object Immutability

Its methods, whether public or private, will not cause its state to change

Page 27: #pugMi - DDD - Value objects

class FullName {

public static function withNameMiddleSurname(...) {

...

}

public static function withNameAndSurname(...) {

...

}

}

Page 28: #pugMi - DDD - Value objects

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

}

Page 29: #pugMi - DDD - Value objects

class FullName {

private function __construct(...) {

// ...

}

public function withMiddleName(StringLiteral $middleName) {

return new self(

$this->name,

$middleName,

$this->surname

);

}

}

Page 30: #pugMi - DDD - Value objects

class FullName {

public function withMiddleName(...) {

$that = clone $this;

$that->name = “Davide”;

$that->middleName = “Sbiellone”;

$that->surname = “Bellettini”;

return $that;

}

}

Page 31: #pugMi - DDD - Value objects

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

Page 32: #pugMi - DDD - Value objects

Characteristics

● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Comparability● Side-Effect-Free behavior with collaborators

Page 33: #pugMi - DDD - Value objects

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

Page 34: #pugMi - DDD - Value objects

Conceptual Whole meaning

Only together do all the attributes form the others, each of the attributes form the complete intended measure or description

Page 35: #pugMi - DDD - Value objects

Conceptual Whole

● {42 euro} has two attributes● The attribute 42 and the attribute euro● Separately these attributes describe something

else or nothing special

Page 36: #pugMi - DDD - Value objects

Characteristics

● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Comparability● Side-Effect-Free behavior with collaborators

Page 37: #pugMi - DDD - Value objects

Replace variable “Value”

$total = 42;

// ...

$total = 4;

Page 38: #pugMi - DDD - Value objects

Replace variable “Value Object”

Is exactly what replacement even when a given Value Object Type is more complex than an integer

Page 39: #pugMi - DDD - Value objects

Replaceability

$speaker = Speaker::withFullName(

“Simone”,

“Gentili”

);

$speaker = Speaker::withFullNameAndNickName(

“Simone”,

“Gentili”,

“Demo”

);

Page 40: #pugMi - DDD - Value objects

Characteristics

● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators

Page 41: #pugMi - DDD - Value objects

Value Objects Equality

● When tho value objects have same values, they are equals

● Values has no identity

Page 42: #pugMi - DDD - Value objects

== 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.”

Page 43: #pugMi - DDD - Value objects

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());

}

Page 44: #pugMi - DDD - Value objects
Page 45: #pugMi - DDD - Value objects

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

Page 46: #pugMi - DDD - Value objects

Characteristics

● Measures, quantifies, or describe● Immutability● Conceptual whole● Replaceability● Equality● Side-Effect-Free behavior with collaborators

Page 47: #pugMi - DDD - Value objects

Side-Effect-Free Behavior

Since no modification occurs when executing a specific operation, that operation is said to be side-effect-free

Page 48: #pugMi - DDD - Value objects

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

Page 49: #pugMi - DDD - Value objects

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

Page 50: #pugMi - DDD - Value objects

Roadmap

● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources

Page 51: #pugMi - DDD - Value objects

Standard Types Expressed as Values

● Currency (Value)● “EUR, USD, ...” (Standard Types)

Page 52: #pugMi - DDD - Value objects

Standard Types Expressed as Values

● HTTP Status Code (Value)● “201, 302, 404, ...” (Standard Types)

Page 53: #pugMi - DDD - Value objects

Standard Types Expressed as Values

Using a Standard Type here helps you avoid bogus currencies (or HTTP Status Codes)

Page 54: #pugMi - DDD - Value objects

Roadmap

● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources

Page 55: #pugMi - DDD - Value objects

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

Page 56: #pugMi - DDD - 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

Page 57: #pugMi - DDD - Value objects

Roadmap

● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources

Page 58: #pugMi - DDD - Value objects

Testing Value Objects

If Values are final classes, cannot be mocked

Page 59: #pugMi - DDD - Value objects

Roadmap

● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources

Page 60: #pugMi - DDD - Value objects

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

Page 61: #pugMi - DDD - Value objects

Roadmap

● Characteristics of a domain concepts● Use of Standard types● Storing Value Objects● Testing Value Objects● Implementations● Resources

Page 62: #pugMi - DDD - Value objects

Github Repositories

● sensorario/kataphp● nicolopignatelli/valueobjects● gws/php-valueobjects● sebastianbergmann/money

Page 63: #pugMi - DDD - Value objects

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

Page 64: #pugMi - DDD - Value 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

Page 65: #pugMi - DDD - Value objects

Contacts

[email protected]

twitter.com/sensorario

instagram.com/sensorario

linkedin.com/in/sensorario

github.com/sensorario

facebook.com/sensorario

Page 66: #pugMi - DDD - Value objects

Q/A

Page 67: #pugMi - DDD - Value objects

Recommended