DDD/CQRS - I must learn to repeat myself

32
repeat myself. I will l earn to repeat myself. I will learn to repeat myself. I wil l learn to repeat myself. I will l earn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will l earn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will learn to repeat myself. I will l earn to repeat myself. I will learn to repeat myself. I will learn to peat myself. I will l earn to repeat myself. I will learn to repeat myself. I will learn to elf. I will learn to repeat myself. I will learn to repeat myself. I will learn to Douglas Reith OSDC 2015 Hobart CQRS / DDD

Transcript of DDD/CQRS - I must learn to repeat myself

Page 1: DDD/CQRS - I must learn to repeat myself

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

repeat myself. I will le

arn to repeat myself. I will le

arn to repeat myself. I will le

arn to

Douglas ReithOSDC 2015 Hobart

CQRS / DDD

Page 2: DDD/CQRS - I must learn to repeat myself

About Me

I use random presentation transitions

< 4 years old = I lived with penguins

Page 3: DDD/CQRS - I must learn to repeat myself

About Me

Tech guy at Engagement Innovation

Glowfeed.com

Page 4: DDD/CQRS - I must learn to repeat myself

What's This About?

Software development and...

A way of thinking

Page 5: DDD/CQRS - I must learn to repeat myself

Basic Truths - Language

We're wired for language

We play 'language games'

Page 6: DDD/CQRS - I must learn to repeat myself

Basic Truths – Software Development

Change is constant

Especially at the start!

'Agile' Methodology

Codebase will GROW

Page 7: DDD/CQRS - I must learn to repeat myself

Basic Truths – Software Development

BIG BALL OF MUD

Page 8: DDD/CQRS - I must learn to repeat myself

What To Do?

It's not a new problem!

GNU/Linux: lots of binaries and authors

Page 9: DDD/CQRS - I must learn to repeat myself

What do they do?

Interfaces/contracts

Isolated behaviour

High internal cohesion, low external coupling

Page 10: DDD/CQRS - I must learn to repeat myself

Building an application...

it's still hard: time and 'resources'

Page 11: DDD/CQRS - I must learn to repeat myself

HELP! - CQRS / DDD

Page 12: DDD/CQRS - I must learn to repeat myself

Domain Driven Design (DDD)

'Domain-driven design (DDD) is an approach to developing software for complex needs by deeply connecting the implementation to an evolving model of the core business concepts.'

http://dddcommunity.org/learning-ddd/what_is_ddd/

'Domain-driven design (DDD) is an approach to developing software for complex needs by deeply connecting the implementation to an evolving model of the core business concepts.'

Page 13: DDD/CQRS - I must learn to repeat myself

Domain Driven Design (DDD)

Ubiquitous language – agreed terms that are well-meaning and consistent within a bounded

context

Develop domain models with a reference to the bounded context

Page 14: DDD/CQRS - I must learn to repeat myself

Domain Driven Design (DDD)

Domain models contain:

Value Objects (VOs)

Entities

Aggregate Roots (ARs)

- that emit Events

Page 15: DDD/CQRS - I must learn to repeat myself

Command and Query Responsibility Segregation (CQRS)

Split your system -

Writing via Commands

Reading via Queries

Page 16: DDD/CQRS - I must learn to repeat myself

CQRS / DDD – The Messaging Marriage

Commands distributed via a Bus

Domain Events distributed via a Bus

Commands and Domain Events are Messages

Page 17: DDD/CQRS - I must learn to repeat myself

The Example

How does all this help?

Page 18: DDD/CQRS - I must learn to repeat myself

The Example

Hi Doug, we need to build a cinema booking application. Our film-goers can book a seat through our application and before the film starts we'll send them an SMS or app notification telling them to get to their seats. Let's send them a notification if they change their password too, that'd be nice :)

Page 19: DDD/CQRS - I must learn to repeat myself

The Example

package cinema­app; 

class User {

     * register(email, password);

     * authenticate(email, password);

     * updatePassword(password);

     * Notification[] notifications;

     * addNotification(notification);

     * Booking[] bookings;

     * addBooking(booking);

}

Page 20: DDD/CQRS - I must learn to repeat myself

More requirements please!

Hi Doug, we want the SMS to go if they haven't logged into the app recently.

Oh and our customers sometimes make group bookings, can you just add that please?

Page 21: DDD/CQRS - I must learn to repeat myself

The Example

package cinema­app; 

class User {

     * register(string email, string password);

     * authenticate(string email, string password);

     * Notification[] notifications;

     * addNotification(Notification notification);

     * Booking[] bookings;

     * addNotification(Notification notification);

}

Hmmmm... add it h

ere?

Page 22: DDD/CQRS - I must learn to repeat myself

Break It Down

Registration/Authentication

Notifications

Bookings

User

User

User

Page 23: DDD/CQRS - I must learn to repeat myself

Now I am repeating myself

package users; 

class User {

     * register(email, password);

     * authenticate(email, password);

     * updatePassword(password);

}

package bookings;

class User {

     * Group group;

     * Booking[] bookings;

     * addBooking(booking);

}

package notifications; 

class User {

     * Notification[] notifications;

     * addNotification(notification);

}

Page 24: DDD/CQRS - I must learn to repeat myself

Packages, packages, packages

'Package' can mean:● Business Domain● PHP Bundle/Library (definitely namespaced)● Bower update● Separate persistence store● Separate source control repository?● Indepedent – you can run unit tests without any other

packages

Page 25: DDD/CQRS - I must learn to repeat myself

But, but, but

But how do we send the notification when they update their password?

How do we know when to send them a notification based on a booking?

Page 26: DDD/CQRS - I must learn to repeat myself

Messages, buses and handlers

package users; 

class User {

     * updatePassword(password) {

          raiseEvent(

              new PasswordUpdated(userId)

          )

       }

}

package bookings;

class User {

     * addBooking(booking) {

          raiseEvent(

              new BookingCreated(userId)

          )

       }

}

package notifications; 

class PasswordChangeListener {

     * handle(PasswordUpdated event);

}

The framework bus will find the right handler/listeners for you.

Page 27: DDD/CQRS - I must learn to repeat myself

Language!

package users; 

class AppUser {

     * register(email, password);

     * authenticate(email, password);

     * updatePassword(password);

}

package bookings;

class FilmGoer {

     * Group group;

     * Booking[] bookings;

     * addBooking(booking);

}

package notifications; 

class Recipient {

     * Notification[] notifications;

     * addNotification(notification);

}

Consequences:● pushing language

up

Page 28: DDD/CQRS - I must learn to repeat myself

Downsides

● Write more code● Lot's of packages/bundles/repositories/databases● Context switching● Developer onboarding – new names for things

(AR's, events etc)● Few reference sites?

Page 29: DDD/CQRS - I must learn to repeat myself

Benefits

● 'Phil, write unit tests for cinema-app' vs 'write them for notifications'

● Dependencies are very clear● Straight forward progressions to microservices when you

want to● 'Deletability'● 'Swapability' – not the right language or persistence store

– don't worry!

Page 30: DDD/CQRS - I must learn to repeat myself

Resources

PHP frameworks:● https://github.com/PhpFriendsOfDdd/state-of-the-union

● https://github.com/szjani/predaddy

A talk I gave previously including a full request/response journey:http://www.slideshare.net/DouglasReith/php-melb-cqrsdddpredaddy

Page 31: DDD/CQRS - I must learn to repeat myself

Thanks

Organisers, Sponsors, and you

János Szurovecz, Greg Young, Eric Evans, Vaughn

Vernon

Page 32: DDD/CQRS - I must learn to repeat myself

Extras?

The Journey