typo3 event sourcing

43
TYPO3 Event Sourcing Oliver Hader 11/2016 Event Sourcing … some new opportunities November 5 th , 2016

Upload: oliver-hader

Post on 16-Jan-2017

428 views

Category:

Science


0 download

TRANSCRIPT

Page 1: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Event Sourcing… some new opportunities

November 5th, 2016

Page 2: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

~basics ~bank account example

~generic modelling ~opportunities

Page 3: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

DDD ~basics

Page 4: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Domain-driven Design• book by Eric Evans, 2003 • robust & maintainable software applications • domain experts & ubiquitous language • toolbox for domain architectures

Page 5: TYPO3 Event Sourcing

Layered Architecture

User Interface Layer

Application Layer

Domain Layer

Infrastructure Layer

Orchestrates application

and domain layer

Encapsulates domain

and data access

Encapsulates

infrastructure concerns

Page 6: TYPO3 Event Sourcing

Aggregates

Customer

Address

Invoice

Car

Wheel

Engine

Garage

CarRental

Survey

car aggregate

customer aggregate

aggregate root

aggregate root

concerns

repairs

interviews

car rental aggregate

Page 7: TYPO3 Event Sourcing

Bounded Contexts

Customer

Car

Fuel

CarRental

car rental bounded context

Car

Wheel

Engine

Garage

garage bounded context

Mechanic

Damage

~\Domain\Model\CarRental ~\Domain\Model\Garage

Page 8: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Entities & Value Objects• make the implicit explicit • make validation part of your domain model • example for bank account number

• $account-­‐>setIban('CH1504842000000000002');  

• $iban  =  new  Iban('CH1504842000000000002');$account-­‐>setIban($iban);

Page 9: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Events• are messages • are modelled in separate classes • concerning something that really happened • used to communicate between components • handle with observer patterns (signal-slot)

• $event  =  new  ReplacedBrokenEngineEvent(…);

Page 10: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

CQRS ~basics

Page 11: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

CQS• CQS - Command Query Separation • actually defined by Bertrand Mayer in 1997 • separate processing in domain model into

• write model - modify state with command • read model - fetch & represent state with query

Page 12: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

CQRS• CQRS - Command Query Responsibility Segregation • more specific & restrictive by Greg Young • segregation between write store & read store • changes trigger updates in read store • visualization just uses data-transfer objects

Page 13: TYPO3 Event Sourcing

CQRS Overview

Page 14: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Event Sourcing ~basics

Page 15: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Event Sourcing• … CQRS continued & in more details • aim to persist events instead of resulting state • event stores provide read/write capabilities • applying all events results to current state again • events are projected into desired formats

Page 16: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Events & Event Store• events are immutable

• cannot be modified • cannot be deleted • legacy events must be handled

• event store is consistent • events are retrieved in correct order • there are no gaps in the event history

Page 17: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Projections• are observers & handle events • interpret and persist events to

• MySQL database • filesystem, e.g. HTML • emit notifications, mails • whatever required format

Page 18: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Materialized View• persist information for accordant requirements • forget about complex JOIN statements • data is represented denormalized • pre-process information for view • read 40 field values vs. just two are shown

Page 19: TYPO3 Event Sourcing

Event Sourcing Overview

Command Handler

Command Bus

Repository

Se

rviceEve

nt P

ub

lishe

r

Even

t Han

dle

r

De

no

rmalize

r

Thin Data Layer

Facade

Domain

DTOs

Commands

Aggregate

Events

EventsEvents Events

SQL

ClientCommands Queries

ExternalEvents

InternalEvents

some queryresults

Page 20: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Why??? ~basics

Page 21: TYPO3 Event Sourcing

TYPO3 Data Architecture

Repository &Reconstitution

Routing

Controller

View

Model

Permission

Persistence

Infrastructure

Frontend Extbase Backend

EditDocumentController

FormEngine

<<TCA>>

FormDataProvider

BackendUserAuthentication

DataHandler

RelationHandler

TypoScriptFrontendController

ActionContoller

AbstractView

AbstractEntity & AbstractValueObject

Repository

Typo3DbBackend

Backend

PersistenceManager

DataMapper

StandaloneView <<custom>>

ConnectionPool

Connection

<<TCA>>

AbstractPlugin

ContentObjectRenderer

Frontend\RequestHandler

Backend\RouteDispatcher

Backend\RequestHandler

MVC\Dispatcher

<<TypoScript>>

<<direct database operations>> <<direct database operations>>

xx

x

Page 22: TYPO3 Event Sourcing

Localization in TYPO3

DataHandlerController

DataHandler

RelationHandler

DatabaseConnection

translate

<<create>>

localize()

copyRecord()

<<create>>

DataHandler

process_datamap()

last_insert_id()

fetch record

record

[x]-processDBdata()

<<create>>

start()

fetch references

references

writeForeignField()

update references

insert record

new_record_id

new_record_id

new_record_id

references

new_record_id

mul

tiple r

ead

& w

rite

proc

esse

s

Page 23: TYPO3 Event Sourcing

Context & Overlays

is translation of

uid 13

sys_language_uid 0

:tt_content

l10n_parent 0

header Message

pid 100

t3ver_wsid 0

t3ver_state 0

t3ver_oid 0

uid 27

sys_language_uid 1

:tt_content

l10n_parent 13

header Nachricht

pid 100

t3ver_wsid 1

t3ver_state 1

t3ver_oid 0

uid 28

sys_language_uid 1

:tt_content

l10n_parent 13

header Nachricht

pid -1

t3ver_wsid 1

t3ver_state -1

t3ver_oid 27

uid 41

sys_language_uid 0

:tt_content

l10n_parent 0

header News

pid -1

t3ver_wsid 1

t3ver_state 0

t3ver_oid 13

is workspaceversion of

is newversion

is defaultversion

is workspaceversion of

is newpalceholder

Page 24: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Why…• is persistence and relation handling different

• extbase cannot persist in workspace context • only back-end has permission layer

• are context information persisted with each record • are record overlays fetched & applied each time

Page 25: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

bank account example

Page 26: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Regular approach• using ExtensionBuilder to kick-start model • AccountController - modify & read information • AccountRepository - modify & read information • Account modelled as aggregate root • Transaction model bound to Aggregate (1:n) • using lazy-loading for Transaction entities

Page 27: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Identify domain events• event storming is a team process • helps to combine knowledge concerning domain • in style of reverse engineering

• working backwards • identify events • identify commands that lead to events • identify rules that are applied to commands

Page 28: TYPO3 Event Sourcing

… during T3DD16

Page 29: TYPO3 Event Sourcing

Bank Account Example

debited money

debitmoney

accountnot closed

balancesufficient

account created

deposit money

accountnot closed

deposited money

createaccount

Page 30: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Demo & Source Code• https://github.com/TYPO3Incubator/

bank_account_example • Configuration • CommandController & ManagementController • Domain Commands & Domain Events • EventRepositories & ProjectionRepositories • Projections & Data-Transfer Objects

Page 31: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

generic modelling

Page 32: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Generic Domain Model• no real models for most core database tables • generic models contain common aspects

• identity (”tt_content:123”) • atomic, direct values • relations to other entities

• ”generic“ is not domain-driven • but this concept is very useful here

Page 33: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Generic Domain Events• created, modified, deleted ~CRUD (without R) • moved, duplicated • hidden, shown ~visibility • translated ~language context • branched, merged ~workspace context • attached, removed, ordered relation ~association

Page 34: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Interceptors• DataHandler ~$tce->process_datamap(); • DatabaseConnection ~INSERT, UPDATE, DELETE • translate actions into generic commands • $GLOBALS['TCA'][…]['ctrl']['eventSourcing']=[        'listenEvents'  =>  true,        'recordEvents'  =>  true,        'projectEvents'  =>  true,

Page 35: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Command Upgrades• tranlate generic to specific domain commands • delegates domain control back to application • back to bank account example

• does not need to implement generic commands • action in back-end form result in real commands

Page 36: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Context & Local Storage• Materialized View on database-level • workspace and language define specific context • each context uses an individual database • information stored in SQLite locally • projections update for each context • it was named Local Storage

Page 37: TYPO3 Event Sourcing

Local Storage

DEFAULTMySQL

- _conn: Driver\Connection

Connection

+ select(...arguments[])+ insert(…arguments[])+ update(…arguments[])+ delete(…arguments[])

ORIGIN

MySQL

- _conn: Driver\Connection

Connection

+ select(...arguments[])+ insert(…arguments[])+ update(…arguments[])+ delete(…arguments[])

DefaultConnection

LocalStorage

workspace-0

SQLite

- _conn: Driver\Connection

Connection

+ select(...arguments[])+ insert(…arguments[])+ update(…arguments[])+ delete(…arguments[])

DefaultConnection

LocalStorage

workspace-1

SQLite

- _conn: Driver\Connection

Connection

+ select(...arguments[])+ insert(…arguments[])+ update(…arguments[])+ delete(…arguments[])

current state future state, context based

assignedto assigned to

Page 38: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Demo & Source Code• https://github.com/TYPO3Incubator/data_handling • Content Editing in back-end • Event Store results • Projections • Command Translations

Page 39: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

opportunities

Page 40: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Projections!• separation between mutation & presentation • events are the new & only true source • everything else can be projected • … and re-projected if it was wrong

• e.g. https://review.typo3.org/#/c/45320/ • trigger index update (Solr, Elasticsearc)

Page 41: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Questions?

Page 42: TYPO3 Event Sourcing

TYPO3 Event Sourcing Oliver Hader 11/2016

Sources• Figures

• “Layered Architecture“ - Buenosvinos, Carlos, Soronellas, Christian und Akbary, Keyvan. 2016. Domain-Driven Design in PHP. Victoria : Leanpub, 2016. ISBN 9780994608413

• ”CQRS Overview“ - Betts, Dominic, et al. 2013. Exploring CQRS and Event Sourcing. Redmond : Microsoft patterns & practices, 2013. ISBN 9781621140160

• ”Event Sourcing Overview“ - Nijhof, Mark. 2013. CQRS, The example. Victoria : Leanpub, 2013. ISBN 9781484102879

• GitHub source codes • https://github.com/TYPO3Incubator/bank_account_example • https://github.com/TYPO3Incubator/data_handling

• Master Thesis on Event Sourcing (German only) • https://get.h4ck3r31.net/T3CRR16-HwWL498EvURCxnnittPoHyNrXkN3xi/

Hader_Oliver_TYPO3_EventSourcing.pdf