Slide 1

Slide 1 text

Introduction to Event Sourcing … and CQRS

Slide 2

Slide 2 text

Vladik Khononov Chief Architect at Internovus http://il.linkedin.com/in/vladikkhononov/ vladikk vladikk http://vladikk.com

Slide 3

Slide 3 text

Information Systems

Slide 4

Slide 4 text

HTML5 AngularJS WPF IOS Flash ExtJS Android Silverlight Responsive webdesign SPA CSS3 User Interface

Slide 5

Slide 5 text

User Interface MySQL MongoDB DynamoDB Neo4j RavenDB PostgreSQL HBase Cassandra Sql Server MariaDB Redis CouchDB Data Access

Slide 6

Slide 6 text

User Interface Data Access “Verbs & Nouns” Domain Driven Design Business Logic

Slide 7

Slide 7 text

Domain Driven Design?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

User Interface Business Logic Data Access

Slide 10

Slide 10 text

– www.dictionary.com Model: A simplified representation of a system or phenomenon. – www.wikipedia.org לדומ: גוציי יטרואת לש תכרעמ תבכרומ , ותרטמש תוקחל תא תכרעמה םיטביהב םייתוהמ . לדומה וניא ראתמ לכ העפות תכרעמב , אלא סחייתמ םיטביהל םירדגומ םימצמוצמו הלש . לדומה ססובמ לע בוריק לש תואיצמה ךרדב לש הטשפה , דוחיא תויושי תומלעתהו םימרוגמ םתעפשהש הניא תיתוהמ .

Slide 11

Slide 11 text

User Interface Business Logic Data Access

Slide 12

Slide 12 text

Domain Model

Slide 13

Slide 13 text

Good Domain Model • Not too much • Not too less • Sweet spot • The information we need • The information we will need

Slide 14

Slide 14 text

Why domain models fail • We absolutely suck at predicting the future • No single model can suit all the use cases • Model transformations are painful

Slide 15

Slide 15 text

Case Study: Customers Management

Slide 16

Slide 16 text

• A customer has customer id number and a name • A customer has contact information: email, phone number • A customer can be in on of the following states: New, CallLater, Converted, NotInterested • A seller has seller id number, name and password • The selling home page contains a list of the customers in the following statuses: • New • CallLater (when scheduled call date is met)

Slide 17

Slide 17 text

• Seller chooses a customer to call from the home page • Seller can change the customer’s status to “Converted”, “Not Interested” • Seller can schedule a future call by setting the future call date. The Customer’s status will change to “CallLater” • Seller can change name, email, and phone number

Slide 18

Slide 18 text

enum Status { New, CallLater, Converted, NotInterested } class Customer { int Id; string Name; Status Status; DateTime? ScheduledCall; string Email; string PhoneNumber; } CREATE TABLE Customers ( ID INTEGER, Name CHAR(40), Email CHAR(40), PhoneNumber CHAR(40), Status INTEGER, ScheduledCall DATETIME, PRIMARY KEY (ID) ) class Seller { int Id; string Name; string Password; } CREATE TABLE Sellers ( ID INTEGER, Name CHAR(40), Password CHAR(40) PRIMARY KEY (ID) )

Slide 19

Slide 19 text

• Seller can find customers by name, email, and/or phone number in the search page • The customers should be found by the current and the past values

Slide 20

Slide 20 text

• If no new customers are available on the home page, it should display customers in the NotInterested status, if it was set more than a 30 days ago

Slide 21

Slide 21 text

• Analysts should be able to review status changes history for each customer - change date and the new status

Slide 22

Slide 22 text

class Seller { int Id; string Name; string Password; } CREATE TABLE Sellers ( ID INTEGER, Name CHAR(40), Password CHAR(40) PRIMARY KEY (ID) ) enum Status { New, CallLater, Converted, NotInterested } class Customer { int Id; string Name; Status Status; DateTime? ScheduledCall; string Email; string PhoneNumber; } CREATE TABLE Customers ( ID INTEGER, Name CHAR(40), Email CHAR(40), PhoneNumber CHAR(40), Status INTEGER, ScheduledCall DATETIME, PRIMARY KEY (ID) )

Slide 23

Slide 23 text

Id Name Email Phone number Status Scheduled Call 10 John [email protected] 04-2342343 New

Slide 24

Slide 24 text

Id Name Email Phone number Status Scheduled Call 10 John [email protected] 04-2342343 CallLater 27/10/2014

Slide 25

Slide 25 text

Id Name Email Phone number Status Scheduled Call 10 John [email protected] 08-9876653 CallLater 27/10/2014

Slide 26

Slide 26 text

Id Name Email Phone number Status Scheduled Call 10 John [email protected] 08-9876653 Converted

Slide 27

Slide 27 text

Id Name Email Phone number Status Scheduled Call 10 John [email protected] 08-9876653 Converted

Slide 28

Slide 28 text

Event Sourcing Capture all changes to an application state as a sequence of events ~ Martin Fowler

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

class StatusChanged : IEvent { Status NewStatus; DateTime CreatedOn; int CreatedBy; } class FutureCallScheduled : IEvent { DateTime ScheduledCallTime; DateTime CreatedOn; int CreatedBy; } class ContactDetailsWereUpdated : IEvent { string NewName; string NewEmail; string NewPhone; DateTime CreatedOn; int CreatedBy; }

Slide 31

Slide 31 text

class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List Events; … void Apply(StatusChanged e) { Status = e.NewStatus; Events.Add(e); } …. …. }

Slide 32

Slide 32 text

class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List Events; … … void Apply(FutureCallScheduled e) { ScheduledCall = e.ScheduledCallTime; Events.Add(e); } … }

Slide 33

Slide 33 text

class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List Events; … … … void Apply(ContactDetailsWereUpdated e) { Name = e.NewName; Email = e.NewEmail; PhoneNumber = e.NewPhoneNumber; Events.Add(e); } }

Slide 34

Slide 34 text

Id Name Email Phone number Status Scheduled Call 10 John [email protected] 08-9876653 Converted

Slide 35

Slide 35 text

1. new StatusChanged(Status.CallLater) 2. new FutureCallScheduled(’27/10/2014’) 3. new ContactDetailsWereUpdated( NewPhoneNumber=’08-9876653’ ) 4. new StatusChanged(Status.Converted)

Slide 36

Slide 36 text

Event Storage Entity Id + New events Entity Id Event1, Event2, Event3, ….

Slide 37

Slide 37 text

class CustomerSearchModel { int Id; List Names; List Emails; List PhoneNumbers; Status Status; DateTime? ScheduledCall; … … … void Apply(ContactDetailsWereUpdated e) { Names.Add(e.NewName); Emails.Add(e.NewEmail); PhoneNumbers.Add(e.NewPhoneNumber); } }

Slide 38

Slide 38 text

class CustomerAnalysisModel { int Id; string Name; string Email; string PhoneNumber; List StatusChanges; DateTime? ScheduledCall; … void Apply(StatusChanged e) { StatusChanges.Add( new StatusChange(e.CreatedOn, e.NewStatus) ); } …. …. }

Slide 39

Slide 39 text

class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List Events; }

Slide 40

Slide 40 text

class CustomerSearchModel { int Id; List Names; List Emails; List PhoneNumbers; Status Status; DateTime? ScheduledCall; }

Slide 41

Slide 41 text

class CustomerAnalysisModel { int Id; string Name; string Email; string PhoneNumber; List StatusChanges; DateTime? ScheduledCall; }

Slide 42

Slide 42 text

Good Domain Model • Not too much • Not too less • Sweet spot • The information we need • The information we might need

Slide 43

Slide 43 text

Why domain models fail • We absolutely suck at predicting the future • No single model can suit all the use cases (e.g.: transactional processing, business intelligence, search) • Model transformations are painful

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Event Storage Entity Id + New events Entity Id Event1, Event2, Event3, ….

Slide 46

Slide 46 text

CQRS Command Query Responsibility Segregation

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

• Event based models • Effective modeling • No future predicting required • Time machine • Retroactive debugging • Infrastructural freedom • Infinite scalability • Easy to scale writes • Easy to scale reads Conclusions

Slide 53

Slide 53 text

Bounded Contexts

Slide 54

Slide 54 text

Domain Types • Core Domain • Subdomain • Generic Subdomain

Slide 55

Slide 55 text

Plexop

Slide 56

Slide 56 text

Campaign Management (Core Domain) Creative Catalog (Subdomain) User Management (Generic Subdomain) Billing (Generic Subdomain)

Slide 57

Slide 57 text

Before you try this at home • Read “Implementing Domain Driven Design” by Vaughn Vernon • Read “Domain Driven Design” by Eric Evans • Follow Greg Young, Udi Dahan, DDD community • Read DDD/CQRS on Google Groups • Join http://meetup.com/DDD-IL :)

Slide 58

Slide 58 text

Questions?

Slide 59

Slide 59 text

http://il.linkedin.com/in/vladikkhononov http://twitter.com/vladikk http://vladikk.com

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content