8. 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);
9. 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(…);
11. 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
12. 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
15. 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
16. 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
17. 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
18. 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
23. 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 workspace
version of
is new
version
is default
version
is workspace
version of
is new
palceholder
24. 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
26. 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
27. 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
32. 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
35. 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
36. 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
37. Local Storage
DEFAULT
MySQL
- _conn: DriverConnection
Connection
+ select(...arguments[])
+ insert(…arguments[])
+ update(…arguments[])
+ delete(…arguments[])
ORIGIN
MySQL
- _conn: DriverConnection
Connection
+ select(...arguments[])
+ insert(…arguments[])
+ update(…arguments[])
+ delete(…arguments[])
DefaultConnection
LocalStorage
workspace-0
SQLite
- _conn: DriverConnection
Connection
+ select(...arguments[])
+ insert(…arguments[])
+ update(…arguments[])
+ delete(…arguments[])
DefaultConnection
LocalStorage
workspace-1
SQLite
- _conn: DriverConnection
Connection
+ select(...arguments[])
+ insert(…arguments[])
+ update(…arguments[])
+ delete(…arguments[])
current state future state, context based
assigned
to
assigned to
38. 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
40. 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)