Slide 1

Slide 1 text

Command AND [e]Mission Control Barney Hanlon [Massive Battlestar Galactica Fan]

Slide 2

Slide 2 text

Firstl y…

Slide 3

Slide 3 text

Celebrating ten years at PHP UK Conference!

Slide 4

Slide 4 text

Thanks to PHP SW for the warm up! @phpsw

Slide 5

Slide 5 text

What do I do?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Foreign currency and payment specialists 60+ IT (developers, DevOps, infrastructure) working on our products Guess what?

Slide 8

Slide 8 text

We’re hiring!

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

The Problem(s) Huge growth led to a huge monolith Finance, especially FinTech, is a rapidly changing market New threats mean changes in compliance and security

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

[Some] Solutions Domain-Driven Design approach Move to micro(ish) services TDD and BDD everywhere

Slide 13

Slide 13 text

Probably nothing new…

Slide 14

Slide 14 text

Domain logic is changing constantly Spotify “model” is improving agility but frameworks/ libraries diverging across squads We have a zillion internal endpoints on our APIs (Still Some) Problems

Slide 15

Slide 15 text

???

Slide 16

Slide 16 text

All right stop Collaborate and listen

Slide 17

Slide 17 text

V anilla Ice says… We’re not going to refactor all these working APIs just to use the same framework. We do need a common way to handle all these changing things though. What lets us plug in new functionality really easily?

Slide 18

Slide 18 text

(Some) B etter Solutions Command buses to tell our domain that we want to do something. Event buses to inform our domain that something has been done. Action-Domain-Responder pattern to control the interaction between the framework and the command bus.

Slide 19

Slide 19 text

B eing Commanding

Slide 20

Slide 20 text

What’s a “Command”? Gang of Four talk about the Command Pattern. DDD refers to Commands within business processes. CQRS

Slide 21

Slide 21 text

CQRS

Slide 22

Slide 22 text

Command Query Responsibility Segregation

Slide 23

Slide 23 text

–Martin Fowler “At its heart is the notion that you can use a different model to update information than the model you use to read information.”

Slide 24

Slide 24 text

Command Pattern

Slide 25

Slide 25 text

– Wikipedia “In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time.”

Slide 26

Slide 26 text

– Wikipedia “This information includes the method name, the object that owns the method and values for the method parameters.”

Slide 27

Slide 27 text

In other words… A (very) simple Data Transfer Object (DTO) Knows nothing about how it will be used Only knows enough to complete the command Operate as a Value Object in your Domain.

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Command Buses

Slide 31

Slide 31 text

– The League of Extraordinary Packages “The term is mostly used when we combine the Command pattern with a service layer.”

Slide 32

Slide 32 text

– The League of Extraordinary Packages “Its job is to take a Command object (which describes what the user wants to do) and match it to a Handler (which executes it). This can help structure your code neatly.”

Slide 33

Slide 33 text

Tactician league/tactician Broadway broadway/broadway Command Bus Libraries

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Event Buses [Emission]

Slide 37

Slide 37 text

Drupal started with “hooks” - simple named pub/sub Zend Framework (0.x - 1.x) had the “Dispatch cycle” SPL Observer/Subject for implementing Observer pattern Symfony Event Dispatcher (et al) Bounce (coming soon) The Evolution of Event buses in PHP

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Comparing Commands and Events

Slide 42

Slide 42 text

Commands and Events are both simple DTOs Beautifully dumb Should be self-validating Commands know (or infer) the one to one relationship with a single handler Comparing Commands and Events

Slide 43

Slide 43 text

Know nothing about the mechanism Only know about their Commands/Events Extremely easy to test Command bus abstracts the Command-Handler relationship Event bus manages the many-to-many Event/Listener relationship Command Handlers AND Event Listeners

Slide 44

Slide 44 text

Hexagonal Architecture “Ports & Adapters”

Slide 45

Slide 45 text

– Alistair Cockburn “Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.”

Slide 46

Slide 46 text

Credit: Fabricio Epaminondas Hexagonal architecture for Java applications

Slide 47

Slide 47 text

–Alistair Cockburn “The application is blissfully ignorant of the nature of the input device.”

Slide 48

Slide 48 text

What is an adapter? HTTP Command Line Queue Listener Testing framework

Slide 49

Slide 49 text

Using the Command Pattern

Slide 50

Slide 50 text

Web HTTP Controller HTTP Adapter Domain Command Bus Using the Command Pattern

Slide 51

Slide 51 text

Why bother with another level of abstraction?

Slide 52

Slide 52 text

“Take as long as you need to understand your domain logic before building a working product. Your product can change, your logic shouldn’t.” – No CEO ever

Slide 53

Slide 53 text

Apps without buses

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Downsides Code duplication is exponential with the number of services involved. HTTP Controllers and console are inherently knowing more about your domain through endpoint names. Infers you know your Domain upfront.

Slide 57

Slide 57 text

EXAMPLE:Domain relativel y unexplored

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Adv ant ages The Controller’s single responsibility is turning a Request into a Command Command Bus easily mocked to test HTTP adapter. Interactions between services within the Domain abstracted away. Can write the HTTP side of the app without knowing any domain services

Slide 60

Slide 60 text

Application Flow

Slide 61

Slide 61 text

Do/Done Cycle Console/Controller as Adapter Create Command - “Do Something” Command Bus Command Handler Create Event - “Something was Done” Event Bus

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Command Event Command Command Command Event Event Event Command Command Command Event Event Event

Slide 66

Slide 66 text

Action Domain Responder

Slide 67

Slide 67 text

Action-Domain-Responder First heard of it via the Slim framework documentation “Web-specific refinement of the MVC pattern” More suited to the closure-based micro frameworks https://github.com/pmjones/adr

Slide 68

Slide 68 text

T aking ADR Further Command/Event buses to perform the interactions

Slide 69

Slide 69 text

Request

Slide 70

Slide 70 text

Request Action

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Request Command Action

Slide 73

Slide 73 text

Command Bus Request Command Action

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Command Bus Request Command Handler Action

Slide 76

Slide 76 text

Command Bus Request Command Service Handler Action

Slide 77

Slide 77 text

Command Bus Request Command Service Handler Service Service Action

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Command Bus Request Command Service Handler Service Service Event Action

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

Command Bus Request Command Service Handler Service Service Event Event Bus Action

Slide 82

Slide 82 text

Command Bus Request Command Service Handler Service Service Event Listeners Event Bus Action

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Command Bus Request Command Service Handler Service Service Event Listeners Event Bus Action Responder

Slide 85

Slide 85 text

Command Bus Request Command Service Handler Service Service Event Listeners Response Event Bus Action Responder

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

Command Bus Request Command Service Handler Service Service Event Listeners Response Event Bus Action Responder

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Switch out the action/responder pair for any adapter Responder can be as smart or dumb as you like Multiple events can populate the Responder CQRS and Command/Events fit well together Self-validating DTOs allow you to “read your own writes” “Succeed or Throw” makes application logic simpler.

Slide 90

Slide 90 text

@shrikeh https://github.com/shrikeh Thank you!

Slide 91

Slide 91 text

Questions

Slide 92

Slide 92 text

@shrikeh https://github.com/shrikeh SO SA Y WE ALL