SlideShare ist ein Scribd-Unternehmen logo
1 von 41
Downloaden Sie, um offline zu lesen
EVENT SOURCING (W PHP)
PIOTR KACAŁA
EVENT SOURCING
ZBUDUJMY SOBIE KOSZYK!
POBIERANIE ZAWARTOŚCI KOSZYKA
mysql> select * from cart_products;
+---------+------------+
| cart_id | product_id |
+---------+------------+
| 1 | 124 |
| 1 | 89 |
+---------+------------+
DODANIE PRODUKTU DO KOSZYKA
POST /CARTS/{CID}/PRODUCTS
INSERT INTO CART_PRODUCTS (…)
USUNIĘCIE PRODUKTU Z KOSZYKA
DELETE /CARTS/{CID}/PRODUCTS/{PID}
DELETE FROM CART_PRODUCTS (…)
DOBRA, MAMY KOSZYK.
PRZYPOMNIJMY
mysql> select * from cart_products;
+---------+------------+
| cart_id | product_id |
+---------+------------+
| 1 | 124 |
| 1 | 89 |
+---------+------------+
POROZMAWIAJMY O PIENIĄDZACH
A CO JEŚLI?
select * from salda_bankowe;
+---------------------+----------------+
| rachunek | ile_pieniazkow |
+---------------------+----------------+
| 4444 3333 2222 1111 | 1000 |
+---------------------+----------------+
PRZECIEŻ PISZE, ŻE 1000.
TRUDNO SIĘ KŁÓCIĆ
+---------------------+----------------+
| rachunek | ile_pieniazkow |
+---------------------+----------------+
| 4444 3333 2222 1111 | 1000 |
+---------------------+----------------+
CZYM JEST EVENT SOURCING?
EVENT STORE
Cart #1 Was
Created
Product #B
Added 

To The Cart
Product #A
Removed 

From The Cart
Product #A
Added 

To The Cart
Cart #1 Was
Checked Out
EVENT STORE
Cart #1 Was
Created
Product #B
Added 

To The Cart
Product #A
Removed 

From The Cart
Product #A
Added 

To The Cart
Cart #1 Was
Checked Out
JAK UPRAWIAĆ EVENT SOURCING (W PHP)?
TRADYCYJNY KOSZYK
class Cart

{

private $id;

private $products = [];



public function add($productId)

{
// changing the state of the cart
$this->products[] = $productId;

}

}
KOSZYK PRODUKUJĄCY EVENTY
class Cart

{

private $id;

private $products = [];

private $raisedEvents = [];



public function add($productId)

{
// changing the state of the cart

$this->products[] = $productId;



// let's raise an event

$this->raisedEvents[] = new ProductAddedToCart($this->id, $productId);

}

}
CartRepository::add(cart)
EVENT STORE
Cart Was
Created
Product #2
Added 

To The Cart
Product #1
Added 

To The Cart
Cart Was
Checked Out
Product #1
Removed 

From The Cart
EVENT STORE
mysql> select * from event_store;
+----+--------------+-------------------------------------------+
| id | aggregate_id | event |
+----+--------------+-------------------------------------------+
| 1 | 1 | CartWasCreated({"cartId":"1"}) |
| 2 | 1 | ProductAddedToCart({"productId":"A"}) |
| 3 | 1 | ProductRemovedFromCart({"productId":"A"}) |
| 4 | 1 | ProductAddedToCart({"productId":"B"}) |
+----+--------------+-------------------------------------------+
ODTWARZANIE AGREGATU KOSZYKA W REPOZYTORIUM
class EventSourcedCartRepository implements CartRepository

{

public function find($aggregateId)

{

$events = $this->eventStore->findEvents($aggregateId);

$cart = new Cart(); // inicjalizacja pustego agregatu



// "nagrywanie" eventów na agregacie cartu

foreach($events as $event) {


// metoda apply() woła odpowiednie metody prywatne

// na podstawie nazwy przekazywanego eventu, np.

// ProductWasAddedToCart => applyProductWasAddedToCart

$cart->apply($event); 

}

}

}
ENCJA
class Cart
{

// this method is called by event store to replay event on the cart
protected function applyProductAddedToCart(ProductAddedToCart $event)

{

$this->products[] = $event->getProductId();

}

}
OBIE METODY
class Cart

{

public function add($productId)

{

if ($this->products > 2) {

throw new CartLimitExceeded();

}



// raise the event

$event = new ProductAddedToCart($this->id, $productId);

$this->raisedEvents[] = $event;



// change the state

$this->applyProductAddedToCart($event);

}



// change the state, this is called by Event Store

public function applyProductAddedToCart(ProductAddedToCart $event)

{

$this->products[] = $event->getProductId();

}

}
CO Z WYDAJNOŚCIĄ?
JAK ROBIĆ ZAPYTANIA?
EVENT STORE
mysql> select * from event_store;
+----+--------------+-------------------------------------------+
| id | aggregate_id | event |
+----+--------------+-------------------------------------------+
| 1 | 1 | CartWasCreated({"cartId":"1"}) |
| 2 | 1 | ProductAddedToCart({"productId":"A"}) |
| 3 | 1 | ProductRemovedFromCart({"productId":"A"}) |
| 4 | 1 | ProductAddedToCart({"productId":"B"}) |
+----+--------------+-------------------------------------------+
CQRS! READ MODEL
MAŁY RECAP
Obiekt 

domeny
Event Store
Listenery
Eventy
TRAFIAJĄ DO
ZAPISYWANE W
PRODUKUJE
PROJEKCJE
class CurrentCartProductsProjector

{

public function applyProductAddedToCart(ProductAddedToCart $event)

{

$this->connection->query('insert into cart_products ...');

}



public function applyProductRemovedFromCart(ProductRemovedFromCart $event)

{

$this->connection->query('delete from cart_products ...');

}

}
POBIERANIE ZAWARTOŚCI KOSZYKA
mysql> select * from cart_products;
+---------+------------+
| cart_id | product_id |
+---------+------------+
| 1 | 124 |
| 1 | 89 |
+---------+------------+
CQRS W PEŁNI SWOJEJ CHWAŁY
Cart::add
Zapis w 

Event Store
Product Was
Added To Cart
Cart State
Projector
SQL
Redis
CO Z ZADANIEM OD PANI MAŁGOSI?
PODSUMOWANIE
PLUSY EVENT SOURCINGU
- HISTORYCZNY STAN APLIKACJI
- DEBUGOWANIE
- PROSTY MODEL ZAPISU
- ODPORNOŚĆ NA KREATYWNOŚĆ BIZNESU
- ŁATWA SKALOWALNOŚĆ
- WSPÓŁGRA Z MIKROSERWISAMI
MINUSY EVENT SOURCINGU
- PRÓG WEJŚCIA (ZMIANA PRZYZWYCZAJEŃ)
- KONIECZNOŚĆ SHARDOWANIA EVENT STORE’U*
- DŁUŻSZY ŁADOWANIE OBIEKTÓW DOMENOWYCH**
- ZWIĘKSZA ZŁOŻONOŚĆ APLIKACJI (VS CRUD)
* NIE DOTYCZY WSZYSTKICH APLIKACJI
** ROZWIĄZANY PRZEZ SNAPSHOTY
/QANDIDATE-LABS/BROADWAY
CIEKAWOSTKA
PYTANIA?
DZIĘKI! d:-)
/WORK

Weitere ähnliche Inhalte

Was ist angesagt?

Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
Sara Torkey
 
Mise en oeuvre des framework de machines et deep learning v1
Mise en oeuvre des framework de machines et deep learning v1 Mise en oeuvre des framework de machines et deep learning v1
Mise en oeuvre des framework de machines et deep learning v1
ENSET, Université Hassan II Casablanca
 

Was ist angesagt? (20)

Cours java v1.4
Cours java v1.4Cours java v1.4
Cours java v1.4
 
Système répartis avec RMI
Système répartis avec RMISystème répartis avec RMI
Système répartis avec RMI
 
Corrige tp java
Corrige tp javaCorrige tp java
Corrige tp java
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and Classes
 
Traitement distribue en BIg Data - KAFKA Broker and Kafka Streams
Traitement distribue en BIg Data - KAFKA Broker and Kafka StreamsTraitement distribue en BIg Data - KAFKA Broker and Kafka Streams
Traitement distribue en BIg Data - KAFKA Broker and Kafka Streams
 
Chapter 8 ooad
Chapter  8 ooadChapter  8 ooad
Chapter 8 ooad
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
 
Angular.pdf
Angular.pdfAngular.pdf
Angular.pdf
 
Support POO Java Deuxième Partie
Support POO Java Deuxième PartieSupport POO Java Deuxième Partie
Support POO Java Deuxième Partie
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Support NodeJS avec TypeScript Express MongoDB
Support NodeJS avec TypeScript Express MongoDBSupport NodeJS avec TypeScript Express MongoDB
Support NodeJS avec TypeScript Express MongoDB
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
Java exception
Java exception Java exception
Java exception
 
Mise en oeuvre des framework de machines et deep learning v1
Mise en oeuvre des framework de machines et deep learning v1 Mise en oeuvre des framework de machines et deep learning v1
Mise en oeuvre des framework de machines et deep learning v1
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 

Ähnlich wie Event sourcing w PHP (by Piotr Kacała)

12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
fasttracksunglass
 
Why SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScriptWhy SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScript
martinlippert
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
Kar Juan
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 

Ähnlich wie Event sourcing w PHP (by Piotr Kacała) (20)

Symfony World - Symfony components and design patterns
Symfony World - Symfony components and design patternsSymfony World - Symfony components and design patterns
Symfony World - Symfony components and design patterns
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
Practical Event Sourcing
Practical Event SourcingPractical Event Sourcing
Practical Event Sourcing
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
 
Why SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScriptWhy SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScript
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda Bagus
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 
Fórum de Software Livre do Serpro RJ 2009
Fórum de Software Livre do Serpro RJ 2009Fórum de Software Livre do Serpro RJ 2009
Fórum de Software Livre do Serpro RJ 2009
 
Introduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingIntroduction to CQRS and Event Sourcing
Introduction to CQRS and Event Sourcing
 
Confoo 2023 - Business logic testing with Behat, Twig and Api Platform
Confoo 2023 - Business logic testing with Behat, Twig and Api PlatformConfoo 2023 - Business logic testing with Behat, Twig and Api Platform
Confoo 2023 - Business logic testing with Behat, Twig and Api Platform
 
[PHPers Summit 2023] Business logic testing
[PHPers Summit 2023] Business logic testing[PHPers Summit 2023] Business logic testing
[PHPers Summit 2023] Business logic testing
 
BDD Revolution - or how we came back from hell
BDD Revolution - or how we came back from hellBDD Revolution - or how we came back from hell
BDD Revolution - or how we came back from hell
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
 
BDD revolution - or how we came back from hell
BDD revolution - or how we came back from hellBDD revolution - or how we came back from hell
BDD revolution - or how we came back from hell
 
When cqrs meets event sourcing
When cqrs meets event sourcingWhen cqrs meets event sourcing
When cqrs meets event sourcing
 
CodingSerbia2014-JavaVSPig
CodingSerbia2014-JavaVSPigCodingSerbia2014-JavaVSPig
CodingSerbia2014-JavaVSPig
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing Strategies
 
[ForumPHP 2023] Lights and shadows of BDD in Sylius (and probably other compa...
[ForumPHP 2023] Lights and shadows of BDD in Sylius (and probably other compa...[ForumPHP 2023] Lights and shadows of BDD in Sylius (and probably other compa...
[ForumPHP 2023] Lights and shadows of BDD in Sylius (and probably other compa...
 

Mehr von GOG.com dev team

The story of GOG.com Cache - PHPers 2014 ( PL )
 The story of GOG.com Cache - PHPers 2014 ( PL ) The story of GOG.com Cache - PHPers 2014 ( PL )
The story of GOG.com Cache - PHPers 2014 ( PL )
GOG.com dev team
 

Mehr von GOG.com dev team (16)

In-depth caching in Varnish - GOG Varnish Meetup, march 2019
In-depth caching in Varnish - GOG Varnish Meetup, march 2019In-depth caching in Varnish - GOG Varnish Meetup, march 2019
In-depth caching in Varnish - GOG Varnish Meetup, march 2019
 
Always up to date, testable and maintainable documentation with OpenAPI
Always up to date, testable and maintainable documentation with OpenAPIAlways up to date, testable and maintainable documentation with OpenAPI
Always up to date, testable and maintainable documentation with OpenAPI
 
Symfony without the framework
Symfony without the frameworkSymfony without the framework
Symfony without the framework
 
Versioning challenges in micro services of Gwent
Versioning challenges in micro services of GwentVersioning challenges in micro services of Gwent
Versioning challenges in micro services of Gwent
 
Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej w grze...
Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej w grze...Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej w grze...
Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej w grze...
 
GWINT: Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej...
GWINT: Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej...GWINT: Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej...
GWINT: Przetwarzanie rozproszone z wykorzystaniem komunikacji asynchronicznej...
 
Jak wydaliśmy wiedźmina, GOG.com IT
Jak wydaliśmy wiedźmina, GOG.com ITJak wydaliśmy wiedźmina, GOG.com IT
Jak wydaliśmy wiedźmina, GOG.com IT
 
Dystrybucja gier w świecie online, przykład architektury webowego systemu ro...
Dystrybucja gier w świecie online, przykład architektury webowego systemu ro...Dystrybucja gier w świecie online, przykład architektury webowego systemu ro...
Dystrybucja gier w świecie online, przykład architektury webowego systemu ro...
 
Wykorzystanie technologii webowych w aplikacjach desktopowych (1)
Wykorzystanie technologii webowych w aplikacjach desktopowych  (1)Wykorzystanie technologii webowych w aplikacjach desktopowych  (1)
Wykorzystanie technologii webowych w aplikacjach desktopowych (1)
 
Design Thinking Workshop: Empathy in the User Experience
Design Thinking Workshop: Empathy in the User ExperienceDesign Thinking Workshop: Empathy in the User Experience
Design Thinking Workshop: Empathy in the User Experience
 
Lean UX - How to start with Lean Startup in User Experience
Lean UX - How to start with Lean Startup in User ExperienceLean UX - How to start with Lean Startup in User Experience
Lean UX - How to start with Lean Startup in User Experience
 
Varnish cache
Varnish cacheVarnish cache
Varnish cache
 
Czym jest złożoność ?
Czym jest złożoność ?Czym jest złożoność ?
Czym jest złożoność ?
 
Intro do Domain Driven Design. ( PL )
Intro do Domain Driven Design. ( PL )Intro do Domain Driven Design. ( PL )
Intro do Domain Driven Design. ( PL )
 
The story of GOG.com Cache - 4developers 2014 ( PL )
The story of GOG.com Cache - 4developers 2014 ( PL )The story of GOG.com Cache - 4developers 2014 ( PL )
The story of GOG.com Cache - 4developers 2014 ( PL )
 
The story of GOG.com Cache - PHPers 2014 ( PL )
 The story of GOG.com Cache - PHPers 2014 ( PL ) The story of GOG.com Cache - PHPers 2014 ( PL )
The story of GOG.com Cache - PHPers 2014 ( PL )
 

Kürzlich hochgeladen

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Kürzlich hochgeladen (20)

%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 

Event sourcing w PHP (by Piotr Kacała)

  • 1. EVENT SOURCING (W PHP) PIOTR KACAŁA
  • 4. POBIERANIE ZAWARTOŚCI KOSZYKA mysql> select * from cart_products; +---------+------------+ | cart_id | product_id | +---------+------------+ | 1 | 124 | | 1 | 89 | +---------+------------+
  • 5. DODANIE PRODUKTU DO KOSZYKA POST /CARTS/{CID}/PRODUCTS INSERT INTO CART_PRODUCTS (…)
  • 6. USUNIĘCIE PRODUKTU Z KOSZYKA DELETE /CARTS/{CID}/PRODUCTS/{PID} DELETE FROM CART_PRODUCTS (…)
  • 8. PRZYPOMNIJMY mysql> select * from cart_products; +---------+------------+ | cart_id | product_id | +---------+------------+ | 1 | 124 | | 1 | 89 | +---------+------------+
  • 10. A CO JEŚLI? select * from salda_bankowe; +---------------------+----------------+ | rachunek | ile_pieniazkow | +---------------------+----------------+ | 4444 3333 2222 1111 | 1000 | +---------------------+----------------+
  • 12. TRUDNO SIĘ KŁÓCIĆ +---------------------+----------------+ | rachunek | ile_pieniazkow | +---------------------+----------------+ | 4444 3333 2222 1111 | 1000 | +---------------------+----------------+
  • 13. CZYM JEST EVENT SOURCING?
  • 14. EVENT STORE Cart #1 Was Created Product #B Added 
 To The Cart Product #A Removed 
 From The Cart Product #A Added 
 To The Cart Cart #1 Was Checked Out
  • 15. EVENT STORE Cart #1 Was Created Product #B Added 
 To The Cart Product #A Removed 
 From The Cart Product #A Added 
 To The Cart Cart #1 Was Checked Out
  • 16. JAK UPRAWIAĆ EVENT SOURCING (W PHP)?
  • 17. TRADYCYJNY KOSZYK class Cart
 {
 private $id;
 private $products = [];
 
 public function add($productId)
 { // changing the state of the cart $this->products[] = $productId;
 }
 }
  • 18. KOSZYK PRODUKUJĄCY EVENTY class Cart
 {
 private $id;
 private $products = [];
 private $raisedEvents = [];
 
 public function add($productId)
 { // changing the state of the cart
 $this->products[] = $productId;
 
 // let's raise an event
 $this->raisedEvents[] = new ProductAddedToCart($this->id, $productId);
 }
 }
  • 20. EVENT STORE Cart Was Created Product #2 Added 
 To The Cart Product #1 Added 
 To The Cart Cart Was Checked Out Product #1 Removed 
 From The Cart
  • 21. EVENT STORE mysql> select * from event_store; +----+--------------+-------------------------------------------+ | id | aggregate_id | event | +----+--------------+-------------------------------------------+ | 1 | 1 | CartWasCreated({"cartId":"1"}) | | 2 | 1 | ProductAddedToCart({"productId":"A"}) | | 3 | 1 | ProductRemovedFromCart({"productId":"A"}) | | 4 | 1 | ProductAddedToCart({"productId":"B"}) | +----+--------------+-------------------------------------------+
  • 22. ODTWARZANIE AGREGATU KOSZYKA W REPOZYTORIUM class EventSourcedCartRepository implements CartRepository
 {
 public function find($aggregateId)
 {
 $events = $this->eventStore->findEvents($aggregateId);
 $cart = new Cart(); // inicjalizacja pustego agregatu
 
 // "nagrywanie" eventów na agregacie cartu
 foreach($events as $event) { 
 // metoda apply() woła odpowiednie metody prywatne
 // na podstawie nazwy przekazywanego eventu, np.
 // ProductWasAddedToCart => applyProductWasAddedToCart
 $cart->apply($event); 
 }
 }
 }
  • 23. ENCJA class Cart {
 // this method is called by event store to replay event on the cart protected function applyProductAddedToCart(ProductAddedToCart $event)
 {
 $this->products[] = $event->getProductId();
 }
 }
  • 24. OBIE METODY class Cart
 {
 public function add($productId)
 {
 if ($this->products > 2) {
 throw new CartLimitExceeded();
 }
 
 // raise the event
 $event = new ProductAddedToCart($this->id, $productId);
 $this->raisedEvents[] = $event;
 
 // change the state
 $this->applyProductAddedToCart($event);
 }
 
 // change the state, this is called by Event Store
 public function applyProductAddedToCart(ProductAddedToCart $event)
 {
 $this->products[] = $event->getProductId();
 }
 }
  • 27. EVENT STORE mysql> select * from event_store; +----+--------------+-------------------------------------------+ | id | aggregate_id | event | +----+--------------+-------------------------------------------+ | 1 | 1 | CartWasCreated({"cartId":"1"}) | | 2 | 1 | ProductAddedToCart({"productId":"A"}) | | 3 | 1 | ProductRemovedFromCart({"productId":"A"}) | | 4 | 1 | ProductAddedToCart({"productId":"B"}) | +----+--------------+-------------------------------------------+
  • 29. MAŁY RECAP Obiekt 
 domeny Event Store Listenery Eventy TRAFIAJĄ DO ZAPISYWANE W PRODUKUJE
  • 30. PROJEKCJE class CurrentCartProductsProjector
 {
 public function applyProductAddedToCart(ProductAddedToCart $event)
 {
 $this->connection->query('insert into cart_products ...');
 }
 
 public function applyProductRemovedFromCart(ProductRemovedFromCart $event)
 {
 $this->connection->query('delete from cart_products ...');
 }
 }
  • 31. POBIERANIE ZAWARTOŚCI KOSZYKA mysql> select * from cart_products; +---------+------------+ | cart_id | product_id | +---------+------------+ | 1 | 124 | | 1 | 89 | +---------+------------+
  • 32. CQRS W PEŁNI SWOJEJ CHWAŁY Cart::add Zapis w 
 Event Store Product Was Added To Cart Cart State Projector SQL Redis
  • 33. CO Z ZADANIEM OD PANI MAŁGOSI?
  • 35. PLUSY EVENT SOURCINGU - HISTORYCZNY STAN APLIKACJI - DEBUGOWANIE - PROSTY MODEL ZAPISU - ODPORNOŚĆ NA KREATYWNOŚĆ BIZNESU - ŁATWA SKALOWALNOŚĆ - WSPÓŁGRA Z MIKROSERWISAMI
  • 36. MINUSY EVENT SOURCINGU - PRÓG WEJŚCIA (ZMIANA PRZYZWYCZAJEŃ) - KONIECZNOŚĆ SHARDOWANIA EVENT STORE’U* - DŁUŻSZY ŁADOWANIE OBIEKTÓW DOMENOWYCH** - ZWIĘKSZA ZŁOŻONOŚĆ APLIKACJI (VS CRUD) * NIE DOTYCZY WSZYSTKICH APLIKACJI ** ROZWIĄZANY PRZEZ SNAPSHOTY
  • 41. /WORK