SlideShare a Scribd company logo
1 of 41
Download to read offline
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

More Related Content

What's hot

Programmation orientée objet en PHP 5
Programmation orientée objet en PHP 5Programmation orientée objet en PHP 5
Programmation orientée objet en PHP 5Kristen Le Liboux
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introductionHasan Kata
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing optionsNir Kaufman
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Introduction to Powershell Version 5
Introduction to Powershell Version 5Introduction to Powershell Version 5
Introduction to Powershell Version 5Nishtha Kesarwani
 
Java - implémentation des concepts objets
Java - implémentation des concepts objetsJava - implémentation des concepts objets
Java - implémentation des concepts objetsJean David Olekhnovitch
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix itRafael Dohms
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation FrameworkCaserta
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHPGuilherme Blanco
 
Kotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxKotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxtakshilkunadia
 

What's hot (20)

Programmation orientée objet en PHP 5
Programmation orientée objet en PHP 5Programmation orientée objet en PHP 5
Programmation orientée objet en PHP 5
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introduction
 
Sets
SetsSets
Sets
 
Introduction au langage SQL
Introduction au langage SQLIntroduction au langage SQL
Introduction au langage SQL
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Plsql
PlsqlPlsql
Plsql
 
MySQL Cheat Sheet
MySQL Cheat SheetMySQL Cheat Sheet
MySQL Cheat Sheet
 
Introduction to Powershell Version 5
Introduction to Powershell Version 5Introduction to Powershell Version 5
Introduction to Powershell Version 5
 
Lazy java
Lazy javaLazy java
Lazy java
 
Java - implémentation des concepts objets
Java - implémentation des concepts objetsJava - implémentation des concepts objets
Java - implémentation des concepts objets
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHP
 
Kotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxKotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptx
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 

Similar to Event sourcing w PHP (by Piotr Kacała)

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Łukasz Chruściel
 
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.pdffasttracksunglass
 
Why SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScriptWhy SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScriptmartinlippert
 
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 BagusWordCamp Indonesia
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介Jace Ju
 
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 2009Fabio Akita
 
Introduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingIntroduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingSamuel ROZE
 
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 PlatformMateusz Zalewski
 
[PHPers Summit 2023] Business logic testing
[PHPers Summit 2023] Business logic testing[PHPers Summit 2023] Business logic testing
[PHPers Summit 2023] Business logic testingMateusz Zalewski
 
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 hellMateusz Zalewski
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Curscatalyst
CurscatalystCurscatalyst
CurscatalystKar Juan
 
[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ć ...Mateusz Zalewski
 
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 hellMateusz Zalewski
 
When cqrs meets event sourcing
When cqrs meets event sourcingWhen cqrs meets event sourcing
When cqrs meets event sourcingManel Sellés
 
CodingSerbia2014-JavaVSPig
CodingSerbia2014-JavaVSPigCodingSerbia2014-JavaVSPig
CodingSerbia2014-JavaVSPigDusan Zamurovic
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesCiaranMcNulty
 
[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...Mateusz Zalewski
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 

Similar to 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
 
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
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
[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...
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 

More from GOG.com dev team

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 2019GOG.com dev team
 
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 OpenAPIGOG.com dev team
 
Symfony without the framework
Symfony without the frameworkSymfony without the framework
Symfony without the frameworkGOG.com dev team
 
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 GwentGOG.com dev team
 
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...GOG.com dev team
 
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...GOG.com dev team
 
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 ITGOG.com dev team
 
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...GOG.com dev team
 
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)GOG.com dev team
 
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 ExperienceGOG.com dev team
 
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 ExperienceGOG.com dev team
 
Czym jest złożoność ?
Czym jest złożoność ?Czym jest złożoność ?
Czym jest złożoność ?GOG.com dev team
 
Intro do Domain Driven Design. ( PL )
Intro do Domain Driven Design. ( PL )Intro do Domain Driven Design. ( PL )
Intro do Domain Driven Design. ( PL )GOG.com dev team
 
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 )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
 

More from 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 )
 

Recently uploaded

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 

Recently uploaded (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 

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