Top Banner
Php Melb Talk CQRS/DDD/predaddy
40

Php melb cqrs-ddd-predaddy

Jul 13, 2015

Download

Software

Douglas Reith
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Php melb cqrs-ddd-predaddy

Php Melb Talk

CQRS/DDD/predaddy

Page 2: Php melb cqrs-ddd-predaddy

About Me

Page 3: Php melb cqrs-ddd-predaddy

Douglas Reith – Technical Guy at Engagement Innovation

Page 4: Php melb cqrs-ddd-predaddy

About Glow

Page 5: Php melb cqrs-ddd-predaddy

Let people talk to businesses with one app:● Give some feedback, hold a conversation;● Get rewarded or donate to charity for you

structured feedback (surveys)

Page 6: Php melb cqrs-ddd-predaddy

What is CQRS?

Page 7: Php melb cqrs-ddd-predaddy

Separate your system into two parts:1) One that deals with client WRITES

(commands) – the Domain side2)And the other that deals with client READS –

the Query side

Page 8: Php melb cqrs-ddd-predaddy

The Domain side doesn't know anything about the Query side.

The Domain is updated via Command objects (DTOs)

The Query sides knows about the Domain (can be limited)

The Query side is updated via Domain Events

Page 9: Php melb cqrs-ddd-predaddy

A Journey

http://didzsa22.deviantart.com/art/The-Mighty-Boosh-Wallpaper-396268100

Page 10: Php melb cqrs-ddd-predaddy

Client: hello!

request

controllers

command bus

commandcommand

domain

command handler

AR

Entity

VO

Entity

VO

dosomething!

post()

event bus

domain eventdomain event

subscribe()

I didsomething!

Object Repository

save(AR)

load(AR)

Getting into the Domain

Page 11: Php melb cqrs-ddd-predaddy

query

event listener

Read model

denormalise

event bus

domain eventdomain event

subscribe()

Object Repository

save(read model)

Getting into somewhere else

http://lonesomeaviary.wordpress.com/

Page 12: Php melb cqrs-ddd-predaddy

Why?

Page 13: Php melb cqrs-ddd-predaddy

On the WRITE side -1)Simplify your domain models to be just about

business rules2)Data encapsulation (less accessor methods);

= Data Integrity + Maintainability

Page 14: Php melb cqrs-ddd-predaddy

On the READ side -1)No (or very little) logic;

2)Aggregate and denormalise if you like;

Page 15: Php melb cqrs-ddd-predaddy

Message Buses mean -1) Leaking the domain is hard - yay;

2)Extensions are easy;3)Avoiding transaction script is good;

4)Later we can do real messaging (MQ);

Page 16: Php melb cqrs-ddd-predaddy

For Glow?

CQRS duplicates your data(there are ways to manage this, however)

Updates/Deletes are not fun when you duplicate your data.

Survey Responses + Conversations = Write and Read but we don't usually Update/Delete

Therefore – good alignment?

Page 17: Php melb cqrs-ddd-predaddy

What is DDD?

http://commons.wikimedia.org/wiki/File:Orange_tabby_cat_sitting_on_fallen_leaves-Hisashi-01.jpg

Page 18: Php melb cqrs-ddd-predaddy

A methodology and a modelling pattern

Page 19: Php melb cqrs-ddd-predaddy

Methodology:● Break down the whole domain into contextual

boundaries;● Be firm on language (ubiquitous language)

Page 20: Php melb cqrs-ddd-predaddy

Modelling pattern -● Aggregate Roots rule

● Entities need to be identified● Value Objects are immutable

Then -● Events

● Repository● Service● Factory

Page 21: Php melb cqrs-ddd-predaddy

What is Event Sourcing?

(awkward silence)

Page 22: Php melb cqrs-ddd-predaddy

predaddy

Page 23: Php melb cqrs-ddd-predaddy

postdaddy

Page 24: Php melb cqrs-ddd-predaddy

predaddy is a framework agnostic PHP CQRS/DDD library

There are others:https://github.com/PhpFriendsOfDdd/state-of-the-union

Page 25: Php melb cqrs-ddd-predaddy

Why predaddy?

Page 26: Php melb cqrs-ddd-predaddy

1)I liked the code style/structure;2)Command bus message callbacks –

progressive enhancement3)Features ->

Page 27: Php melb cqrs-ddd-predaddy

predaddy features -● Direct Command Bus

● Flexible Transaction Control● Testing fixtures

● Command validation

Page 28: Php melb cqrs-ddd-predaddy

Check out my fork for a bundle of fun https://bitbucket.org/douglas_reith/riverline-predaddy-bundle

(update branch)

But sorry, predaddy 2.2 not 3

Page 29: Php melb cqrs-ddd-predaddy

Where are we at?

Page 30: Php melb cqrs-ddd-predaddy

CQRS/DDD-lite:● On the Query side inject a domain repository;

● Everything is in one process;● Modelling is immature;

● Over reliance on doctrine lazy loading (context breaker)

Page 31: Php melb cqrs-ddd-predaddy

CQRS/DDD-lite:Is it YUK?

http://dooolittle.deviantart.com/art/Colourful-Dog-130580130

Page 32: Php melb cqrs-ddd-predaddy

DDD/CQRS-lite:Not ideal but you can progressively enhance -● Swap out the domain repo backing the read

model and swap in the query repo;● Start selecting messages

(COMMANDS/EVENTS) to be posted onto a Queue;

● Get better at DDD – do some refactoring, create separate repos, bundles, and refine your

language and contexts

Page 33: Php melb cqrs-ddd-predaddy

Tricks -● Use domain event inheritence to listen to them

all;● You can post a command from an event listener;

● You can post a command from a command handler!

● Using NoSQL is a help

Page 34: Php melb cqrs-ddd-predaddy

I've got questions -● Hydrating scalar DTO's (commands) when

entering command handlers? e.g. Dates?

Page 35: Php melb cqrs-ddd-predaddy

I've got questions -● REST versus CQRS/DDD. Do we need

impedance matching?➢ Nesting resources and ARs;

➢ Add 'command' string to Accept header;

Page 36: Php melb cqrs-ddd-predaddy

I've got questions -● Name spaces, folder structure, repositories,

bundles● CQRS sends you down Domain/Query, but really

you can be more imaginative than that!

Page 37: Php melb cqrs-ddd-predaddy

Warnings:● Performance – not given a good test, needs

work;● Developer velocity (files! files! files!) ;

● Reference sites/implementations;● Complicated doctrine configuration and niggly

bugs

Page 38: Php melb cqrs-ddd-predaddy

More Warnings:● Events and Commands and Events and

Commands and... intent is less clear, sometimes best just let the domain do it's job directly

Page 39: Php melb cqrs-ddd-predaddy

Don't forget the goodness:Light DTO's (events and commands), bounded contexts/domains, messaging buses = SOA in

memory!

Jump around, punch the air!

Page 40: Php melb cqrs-ddd-predaddy

More please:Search for DDD or CQRS – there are a few

articles about PHP and many more in .NET and Java.