Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Technically DDD

Slide 3

Slide 3 text

Pim Elshoff developer.procurios.com @pelshoff

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Questions?

Slide 7

Slide 7 text

Context Technical building blocks Quiz Case study I Case study II

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Context Technical building blocks Quiz Case study I Case study II

Slide 10

Slide 10 text

Value objects

Slide 11

Slide 11 text

final class Name { private $lastName; private $firstName; private $insertion; public function __construct(string $lastName, string $firstName, string $insertion) { $this->lastName = $lastName; $this->firstName = $firstName; $this->insertion = $insertion; $this->nameMustHaveLastName (); } private function nameMustHaveLastName (): void { if (!$this->lastName) { throw InvalidName::becauseLastNameIsMissing() ; } } public function getLastName(): string { /**/ } public function getFirstName(): string { /**/ } public function getInsertion(): string { /**/ } }

Slide 12

Slide 12 text

final class EmailAddress { private $emailAddress; public function __construct(string $emailAddress) { $this->emailAddress = $emailAddress; $this->emailAddressMustBeAnActualEmailAddress (); } private function emailAddressMustBeAnActualEmailAddress (): void { if (!filter_var($this->emailAddress, FILTER_VALIDATE_EMAIL)) { throw InvalidEmailAddress::becauseThisIsNotAnEmailAddress() ; } } public function asString(): string { return $this->emailAddress; } }

Slide 13

Slide 13 text

Value objects ● Express a value ● Define allowed values ● Are immutable ● Are easy to test

Slide 14

Slide 14 text

Entities

Slide 15

Slide 15 text

final class Attendee { private $id; private $name; private $emailAddress; public function __construct(Uuid $id, Name $name, EmailAddress $emailAddress) { $this->id = $id; $this->name = $name; $this->emailAddress = $emailAddress; } public function getId(): Uuid { /**/ } public function getName(): Name { /**/ } public function setName(Name $name): Attendee { /**/ } public function getEmailAddress (): EmailAddress { return $this->emailAddress; } public function setEmailAddress (EmailAddress $emailAddress): Attendee { $this->emailAddress = $emailAddress; return $this; }

Slide 16

Slide 16 text

Entities ● Have identity ● Are more than their attributes ● Evolve over time ● Are slightly harder to test

Slide 17

Slide 17 text

Services

Slide 18

Slide 18 text

final class Registration { private $repository; public function __construct(AttendeeRepository $repository) { $this->repository = $repository; } public function registerNew(Attendee $attendee): void { $this->emailAddressMustBeUnique ($attendee->getEmailAddress ()); $this->repository->add($attendee); } private function emailAddressMustBeUnique (EmailAddress $emailAddress): void { try { $this->repository->findByEmailAddress ($emailAddress); } catch (AttendeeNotFound $e) { return; } throw RegistrationFailed::becauseEmailAddressIsNotUnique() ; } }

Slide 19

Slide 19 text

Services ● Have no identity or attributes (Are not a “thing”) ● Tackle cross-concern operations ● Are harder to test

Slide 20

Slide 20 text

Repositories

Slide 21

Slide 21 text

Repositories ● Are a collection of (almost) all objects of a type

Slide 22

Slide 22 text

Context Technical building blocks Quiz Case study I Case study II

Slide 23

Slide 23 text

final class Address

Slide 24

Slide 24 text

final class BicycleService

Slide 25

Slide 25 text

final class StreetAddress

Slide 26

Slide 26 text

final class Investment

Slide 27

Slide 27 text

Context Technical building blocks Quiz Case study I Case study II

Slide 28

Slide 28 text

final class Meeting { private $meetingId; private $title; private $description; private $code; private $startDate; private $endDate; private $startTime; private $endTime; private $isPublished; private $subTitle; private $program; public function __construct(Uuid $meetingId, string $title, string $description, string $code, string $startDate, string $endDate, string $startTime, string $endTime, bool $isPublished, string $subTitle, array $program) { $this->meetingId = $meetingId; $this->title = $title; $this->description = $description; $this->code = $code; $this->startDate = $startDate; $this->endDate = $endDate; $this->startTime = $startTime; $this->endTime = $endTime; // ... } }

Slide 29

Slide 29 text

new Meeting(Uuid:: generate(), 'This is a test' , 'This is a test description' , 'M01', '2016-09-29', '2016-09-29', '09:00', '18:00', false, 'This is a test sub title' , [ [ 'date' => '2016-09-29', 'startTime' => '09:00', 'endTime' => '09:30', 'title' => 'Opening', 'room' => 'White room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:30', 'title' => 'Intro OOP', 'room' => 'Black room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:00', 'title' => 'Intro FP', 'room' => 'White room', ], ] )

Slide 30

Slide 30 text

Meetings cannot end before they start

Slide 31

Slide 31 text

The Approach™ 1. Implement in Entity 2. Extract Value Object 3. Refactor Value Object

Slide 32

Slide 32 text

final class Meeting { public function __construct(/**/) { // .. $this->meetingCannotEndBeforeStart (); } private function meetingCannotEndBeforeStart (): void { if ($this->startDate < $this->endDate) { return; } if ($this->startDate > $this->endDate) { throw InvalidMeeting::becauseMeetingEndsBeforeStarting() ; } if ($this->startTime > $this->endTime) { throw InvalidMeeting::becauseMeetingEndsBeforeStarting() ; } } }

Slide 33

Slide 33 text

final class Meeting { private $meetingId; private $title; private $description; private $code; private $duration; private $isPublished; private $subTitle; private $program; public function __construct(Uuid $meetingId, string $title, string $description, string $code, MeetingDuration $duration, bool $isPublished, string $subTitle, array $program) { $this->meetingId = $meetingId; $this->title = $title; $this->description = $description; $this->code = $code; $this->duration = $duration; $this->isPublished = $isPublished; $this->subTitle = $subTitle; $this->program = $program; } }

Slide 34

Slide 34 text

final class MeetingDuration { private $startDate; private $endDate; private $startTime; private $endTime; public function __construct(string $startDate, string $endDate, string $startTime, string $endTime) { $this->startDate = $startDate; $this->endDate = $endDate; $this->startTime = $startTime; $this->endTime = $endTime; $this->meetingCannotEndBeforeStart (); } private function meetingCannotEndBeforeStart (): void { if ($this->startDate < $this->endDate) { return; } if ($this->startDate > $this->endDate) { throw InvalidMeetingDuration::becauseDurationEndsBeforeStarting() ; } if ($this->startTime > $this->endTime) { throw InvalidMeetingDuration::becauseDurationEndsBeforeStarting() ; } } }

Slide 35

Slide 35 text

final class MeetingDuration { private $start; private $end; public function __construct(DateTimeImmutable $start, DateTimeImmutable $end) { $this->start = $start; $this->end = $end; $this->meetingCannotEndBeforeStart (); } private function meetingCannotEndBeforeStart (): void { if ($this->start > $this->end) { throw InvalidMeetingDuration::becauseDurationEndsBeforeStarting() ; } } }

Slide 36

Slide 36 text

new Meeting(Uuid:: generate(), 'This is a test' , 'This is a test description' , 'M01', new MeetingDuration( new DateTimeImmutable( '2016-09-29' . ' ' . '09:00'), new DateTimeImmutable( '2016-09-29' . ' ' . '18:00') ), false, 'This is a test sub title' , [ [ 'date' => '2016-09-29', 'startTime' => '09:00', 'endTime' => '09:30', 'title' => 'Opening', 'room' => 'White room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:30', 'title' => 'Intro OOP', 'room' => 'Black room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:00', 'title' => 'Intro FP', 'room' => 'White room',

Slide 37

Slide 37 text

Context Technical building blocks Quiz Case study I Case study II

Slide 38

Slide 38 text

Program slots cannot occur in the same room at the same time

Slide 39

Slide 39 text

[ [ 'date' => '2016-09-29', 'startTime' => '09:00', 'endTime' => '09:30', 'title' => 'Opening', 'room' => 'White room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:30', 'title' => 'Intro OOP', 'room' => 'Black room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:00', 'title' => 'Intro FP', 'room' => 'White room', ], ]

Slide 40

Slide 40 text

private function programSlotsCannotOccurInTheSameRoomAtTheSameTime (): void { foreach ($this->program as $index => $slot) { foreach (array_slice($this->program, $index + 1) as $comparison) { if ($slot['room'] !== $comparison['room']) { continue; } if ($slot['date'] !== $comparison['date']) { continue; } if ($slot['startTime'] >= $comparison['endTime']) { continue; } if ($slot['endTime'] <= $comparison['startTime']) { continue; } throw InvalidProgram::becauseProgramSlotsOverlap() ; } } }

Slide 41

Slide 41 text

final class Meeting { private $meetingId; private $title; private $description; private $code; private $duration; private $isPublished; private $subTitle; private $program; public function __construct(Uuid $meetingId, string $title, string $description, string $code, MeetingDuration $duration, bool $isPublished, string $subTitle, Program $program) { $this->meetingId = $meetingId; $this->title = $title; $this->description = $description; $this->code = $code; $this->duration = $duration; $this->isPublished = $isPublished; $this->subTitle = $subTitle; $this->program = $program; } }

Slide 42

Slide 42 text

final class Program { private $program; public function __construct(array $program) { $this->program = $program; $this->programSlotsCannotOccurInTheSameRoomAtTheSameTime (); } private function programSlotsCannotOccurInTheSameRoomAtTheSameTime (): void { foreach ($this->program as $index => $slot) { foreach (array_slice($this->program, $index + 1) as $comparison) { if ($slot['room'] !== $comparison['room']) { continue; } if ($slot['date'] !== $comparison['date']) { continue; } if ($slot['startTime'] >= $comparison['endTime']) { continue; } if ($slot['endTime'] <= $comparison['startTime']) { continue; } throw InvalidProgram::becauseProgramSlotsOverlap() ; } } // ..

Slide 43

Slide 43 text

final class Program { private $program; /** @param Slot[] $program */ public function __construct(array $program) { $this->program = $program; $this->programSlotsCannotOccurInTheSameRoomAtTheSameTime (); } private function programSlotsCannotOccurInTheSameRoomAtTheSameTime (): void { foreach ($this->program as $index => $thisSlot) { foreach (array_slice($this->program, $index + 1) as $thatSlot) { if ($thisSlot->overlapsWith($thatSlot)) { throw InvalidProgram::becauseProgramSlotsOverlap() ; } } } } }

Slide 44

Slide 44 text

final class Slot { private $duration; private $title; private $room; public function __construct(MeetingDuration $duration, string $title, string $room) { $this->duration = $duration; $this->title = $title; $this->room = $room; } public function overlapsWith(Slot $that): bool { return $this->room === $that->room && $this->duration->overlapsWith($that->duration); } }

Slide 45

Slide 45 text

final class MeetingDuration { private $start; private $end; public function __construct(DateTimeImmutable $start, DateTimeImmutable $end) { $this->start = $start; $this->end = $end; $this->meetingCannotEndBeforeStart (); } private function meetingCannotEndBeforeStart (): void { if ($this->start >= $this->end) { throw InvalidMeetingDuration::becauseDurationEndsBeforeStarting() ; } } public function overlapsWith(MeetingDuration $that): bool { return $this->start >= $that->start && $this->start <= $that->end || $this->end >= $that->start && $this->end <= $that->end || $that->start >= $this->start && $that->start <= $this->end || $that->end >= $this->start && $that->end <= $this->end; } }

Slide 46

Slide 46 text

final class MeetingDuration { private $start; private $end; public function __construct(DateTimeImmutable $start, DateTimeImmutable $end) { $this->start = $start; $this->end = $end; $this->meetingCannotEndBeforeStart (); } private function meetingCannotEndBeforeStart (): void { if ($this->start >= $this->end) { throw InvalidMeetingDuration::becauseDurationEndsBeforeStarting() ; } } public function overlapsWith(MeetingDuration $that): bool { return $that->contains($this->start) || $that->contains($this->end) || $this->contains($that->start) || $this->contains($that->end); } private function contains(DateTimeImmutable $date): bool { return $date >= $this->start && $date <= $this->end; } }

Slide 47

Slide 47 text

final class MeetingDuration { private $start; private $end; public function __construct(DateTimeImmutable $start, DateTimeImmutable $end) { $this->start = $start; $this->end = $end; $this->meetingCannotEndBeforeStart (); } private function meetingCannotEndBeforeStart (): void { if ($this->start >= $this->end) { throw InvalidMeetingDuration::becauseDurationEndsBeforeStarting() ; } } public function overlapsWith(MeetingDuration $that): bool { return !($this->start > $that->end || $that->start > $this->end); } }

Slide 48

Slide 48 text

final class MeetingDuration { private $start; private $end; public function __construct(DateTimeImmutable $start, DateTimeImmutable $end) { $this->start = $start; $this->end = $end; $this->meetingCannotEndBeforeStart (); } private function meetingCannotEndBeforeStart (): void { if ($this->start >= $this->end) { throw InvalidMeetingDuration::becauseDurationEndsBeforeStarting() ; } } public function overlapsWith(MeetingDuration $that): bool { return !$this->before($that) && !$that->before($this); } private function before(MeetingDuration $that): bool { return $that->start > $this->end; } }

Slide 49

Slide 49 text

final class SlotDuration { private $start; private $end; public function __construct(DateTimeImmutable $start, DateTimeImmutable $end) { $this->start = $start; $this->end = $end; $this->slotCannotEndBeforeStart (); $this->slotMustStartAndEndOnTheSameDay (); } private function slotCannotEndBeforeStart (): void { /**/ } private function slotMustStartAndEndOnTheSameDay (): void { if ($this->start->diff($this->end)->days >= 1) { throw InvalidSlotDuration::becauseSlotEndsOnDifferentDay() ; } } public function overlapsWith(SlotDuration $that): bool { return !$this->before($that) && !$that->before($this); } private function before(SlotDuration $that): bool { return $that->start >= $this->end; } }

Slide 50

Slide 50 text

new Meeting(Uuid:: generate(), 'This is a test' , 'This is a test description' , 'M01', '2016-09-29', '2016-09-29', '09:00', '18:00', false, 'This is a test sub title' , [ [ 'date' => '2016-09-29', 'startTime' => '09:00', 'endTime' => '09:30', 'title' => 'Opening', 'room' => 'White room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:30', 'title' => 'Intro OOP', 'room' => 'Black room', ], [ 'date' => '2016-09-29', 'startTime' => '09:30', 'endTime' => '10:00', 'title' => 'Intro FP', 'room' => 'White room', ], ] )

Slide 51

Slide 51 text

new Meeting(Uuid::generate(), 'This is a test', 'This is a test description', 'M01', new MeetingDuration( new DateTimeImmutable('2016-09-29' . ' ' . '09:00'), new DateTimeImmutable('2016-09-29' . ' ' . '18:00') ), false, 'This is a test sub title', new Program([ new Slot( new SlotDuration( new DateTimeImmutable('2016-09-29' . ' ' . '09:00'), new DateTimeImmutable('2016-09-29' . ' ' . '09:30') ), 'Opening', 'White room' ), new Slot( new SlotDuration( new DateTimeImmutable('2016-09-29' . ' ' . '09:30'), new DateTimeImmutable('2016-09-29' . ' ' . '10:30') ), 'Intro OOP', 'Black room' ), new Slot( new SlotDuration( new DateTimeImmutable('2016-09-29' . ' ' . '09:30'), new DateTimeImmutable('2016-09-29' . ' ' . '10:00') ), 'Intro FP', 'White room' ), ]) )

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Pim Elshoff developer.procurios.com @pelshoff

Slide 54

Slide 54 text

Program slots must occur within the duration of the meeting

Slide 55

Slide 55 text

final class Meeting { private $meetingId; private $title; private $description; private $code; private $duration; private $isPublished; private $subTitle; private $program; public function __construct(Uuid $meetingId, string $title, string $description, string $code, MeetingDuration $duration, bool $isPublished, string $subTitle, Program $program) { $this->meetingId = $meetingId; $this->title = $title; $this->description = $description; $this->code = $code; $this->duration = $duration; $this->isPublished = $isPublished; $this->subTitle = $subTitle; $this->program = $program; } }

Slide 56

Slide 56 text

final class Meeting { // ... public function __construct(Uuid $meetingId, string $title, string $description, string $code, MeetingDuration $duration, bool $isPublished, string $subTitle, Program $program) { // ... $this->programSlotsMustOccurWithinDuration (); } private function programSlotsMustOccurWithinDuration (): void { if ($this->program->occursOutsideOf ($this->duration)) { throw InvalidMeeting:: becauseProgramOccursOutsideOfDuration(); } } }

Slide 57

Slide 57 text

final class Program { // ... public function occursOutsideOf (MeetingDuration $duration): bool { foreach ($this->program as $slot) { if ($slot->occursOutsideOf ($duration)) { return true; } } return false; } }

Slide 58

Slide 58 text

final class Slot { // ... public function occursOutsideOf (MeetingDuration $duration): bool { return $this->duration->occursOutsideOf ($duration); } }

Slide 59

Slide 59 text

final class SlotDuration { private $start; private $end; // ... public function occursOutsideOf (MeetingDuration $duration): bool { return $this->start < $duration->getStart() || $this->end > $duration->getEnd(); } }

Slide 60

Slide 60 text

final class Meeting { private $meetingId; private $title; private $description; private $code; private $duration; private $isPublished; private $subTitle; private $program; public function __construct(Uuid $meetingId, string $title, string $description, string $code, MeetingDuration $duration, bool $isPublished, string $subTitle, Program $program) { $this->meetingId = $meetingId; $this->title = $title; $this->description = $description; $this->code = $code; $this->duration = $duration; $this->isPublished = $isPublished; $this->subTitle = $subTitle; $this->program = $program; } }

Slide 61

Slide 61 text

final class Meeting { private $meetingId; private $title; private $description; private $code; private $isPublished; private $subTitle; private $program; public function __construct(UuidInterface $meetingId, string $title, string $description , string $code, bool $isPublished, string $subTitle, Program $program ) { $this->meetingId = $meetingId; $this->title = $title; $this->description = $description; $this->code = $code; $this->isPublished = $isPublished; $this->subTitle = $subTitle; $this->program = $program; } }

Slide 62

Slide 62 text

final class Program { private $duration; private $program; public function __construct(MeetingDuration $duration, array $program) { $this->duration = $duration; $this->program = $program; $this->programSlotsMustOccurWithinDuration (); $this->programSlotsCannotOccurInTheSameRoomAtTheSameTime (); } private function programSlotsMustOccurWithinDuration (): void { foreach ($this->program as $slot) { if ($slot->occursOutsideOf ($this->duration)) { throw InvalidProgram:: becauseProgramOccursOutsideOfDuration(); } } } private function programSlotsCannotOccurInTheSameRoomAtTheSameTime (): void { // ... } }

Slide 63

Slide 63 text

No content