typo3 event sourcing

Post on 16-Jan-2017

423 Views

Category:

Science

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

TYPO3 Event Sourcing Oliver Hader 11/2016

Event Sourcing… some new opportunities

November 5th, 2016

TYPO3 Event Sourcing Oliver Hader 11/2016

~basics ~bank account example

~generic modelling ~opportunities

TYPO3 Event Sourcing Oliver Hader 11/2016

DDD ~basics

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

Layered Architecture

User Interface Layer

Application Layer

Domain Layer

Infrastructure Layer

Orchestrates application

and domain layer

Encapsulates domain

and data access

Encapsulates

infrastructure concerns

Aggregates

Customer

Address

Invoice

Car

Wheel

Engine

Garage

CarRental

Survey

car aggregate

customer aggregate

aggregate root

aggregate root

concerns

repairs

interviews

car rental aggregate

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

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);

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(…);

TYPO3 Event Sourcing Oliver Hader 11/2016

CQRS ~basics

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

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

CQRS Overview

TYPO3 Event Sourcing Oliver Hader 11/2016

Event Sourcing ~basics

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

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

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

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

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

TYPO3 Event Sourcing Oliver Hader 11/2016

Why??? ~basics

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

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

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

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

TYPO3 Event Sourcing Oliver Hader 11/2016

bank account example

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

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

… during T3DD16

Bank Account Example

debited money

debitmoney

accountnot closed

balancesufficient

account created

deposit money

accountnot closed

deposited money

createaccount

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

TYPO3 Event Sourcing Oliver Hader 11/2016

generic modelling

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

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

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,

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

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

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

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

TYPO3 Event Sourcing Oliver Hader 11/2016

opportunities

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)

TYPO3 Event Sourcing Oliver Hader 11/2016

Questions?

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

top related