SlideShare a Scribd company logo
1 of 61
Introduction to Event Sourcing
… and CQRS
Vladik Khononov
Chief Architect at Internovus
http://il.linkedin.com/in/vladikkhononov/
vladikk vladikkhttp://vladikk.com
Information Systems
HTML5AngularJSWPFIOSFlashExtJSAndroidSilverlight
Responsive
webdesign
SPACSS3
User
Interface
User
Interface
MySQLMongoDBDynamoDBNeo4jRavenDBPostgreSQLHBaseCassandraSql ServerMariaDBRedisCouchDB
Data
Access
User
Interface
Data
Access
“Verbs & Nouns”Domain Driven Design
Business
Logic
Domain Driven Design?
User
Interface
Business
Logic
Data
Access
– www.dictionary.com
Model: A simplified representation of a system or
phenomenon.
– www.wikipedia.org
‫:מודל‬ ‫ייצוג‬‫תאורטי‬‫של‬‫מערכת‬‫מורכבת‬ , ‫שמטרתו‬
‫לחקות‬‫את‬‫המערכת‬‫בהיבטים‬‫מהותיים‬ . ‫המודל‬‫אינו‬
‫מתאר‬‫כל‬‫תופעה‬‫במערכת‬ , ‫אלא‬‫מתייחס‬‫להיבטים‬
‫מוגדרים‬‫ומצומצמים‬‫שלה‬ . ‫המודל‬‫מבוסס‬‫על‬‫קירוב‬‫של‬
‫המציאות‬‫בדרך‬‫של‬‫הפשטה‬ , ‫איחוד‬‫ישויות‬‫והתעלמות‬
‫מגורמים‬‫שהשפעתם‬‫אינה‬‫מהותית‬ .
User
Interface
Business
Logic
Data
Access
Domain Model
Good Domain Model
• Not too much
• Not too less
• Sweet spot
• The information we need
• The information we will need
Why domain models fail
• We absolutely suck at predicting the future
• No single model can suit all the use cases
• Model transformations are painful
Case Study:
Customers Management
• 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)
• 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
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)
)
• 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
• 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
• Analysts should be able to review status changes
history for each customer - change date and the
new status
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)
)
Id Name Email Phone number Status
Scheduled
Call
10 John john@gmail.com 04-2342343 New
Id Name Email Phone number Status
Scheduled
Call
10 John john@gmail.com 04-2342343 CallLater 27/10/2014
Id Name Email Phone number Status
Scheduled
Call
10 John john@gmail.com 08-9876653 CallLater 27/10/2014
Id Name Email Phone number Status
Scheduled
Call
10 John john@gmail.com 08-9876653 Converted
Id Name Email Phone number Status
Scheduled
Call
10 John john@gmail.com 08-9876653 Converted
Event Sourcing
Capture all changes to an application
state as a sequence of events
~ Martin Fowler
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;
}
class Customer {
int Id;
string Name;
string Email;
string PhoneNumber;
Status Status;
DateTime? ScheduledCall;
List<IEvent> Events;
…
void Apply(StatusChanged e) {
Status = e.NewStatus;
Events.Add(e);
}
….
….
}
class Customer {
int Id;
string Name;
string Email;
string PhoneNumber;
Status Status;
DateTime? ScheduledCall;
List<IEvent> Events;
…
…
void Apply(FutureCallScheduled e) {
ScheduledCall = e.ScheduledCallTime;
Events.Add(e);
}
…
}
class Customer {
int Id;
string Name;
string Email;
string PhoneNumber;
Status Status;
DateTime? ScheduledCall;
List<IEvent> Events;
…
…
…
void Apply(ContactDetailsWereUpdated e) {
Name = e.NewName;
Email = e.NewEmail;
PhoneNumber = e.NewPhoneNumber;
Events.Add(e);
}
}
Id Name Email Phone number Status
Scheduled
Call
10 John john@gmail.com 08-9876653 Converted
1. new StatusChanged(Status.CallLater)
2. new FutureCallScheduled(’27/10/2014’)
3. new ContactDetailsWereUpdated(
NewPhoneNumber=’08-9876653’
)
4. new StatusChanged(Status.Converted)
Event Storage
Entity Id + New events
Entity IdEvent1,
Event2,
Event3,
….
class CustomerSearchModel {
int Id;
List<string> Names;
List<string> Emails;
List<string> PhoneNumbers;
Status Status;
DateTime? ScheduledCall;
…
…
…
void Apply(ContactDetailsWereUpdated e) {
Names.Add(e.NewName);
Emails.Add(e.NewEmail);
PhoneNumbers.Add(e.NewPhoneNumber);
}
}
class CustomerAnalysisModel {
int Id;
string Name;
string Email;
string PhoneNumber;
List<StatusChange> StatusChanges;
DateTime? ScheduledCall;
…
void Apply(StatusChanged e) {
StatusChanges.Add(
new StatusChange(e.CreatedOn, e.NewStatus)
);
}
….
….
}
class Customer {
int Id;
string Name;
string Email;
string PhoneNumber;
Status Status;
DateTime? ScheduledCall;
List<IEvent> Events;
}
class CustomerSearchModel {
int Id;
List<string> Names;
List<string> Emails;
List<string> PhoneNumbers;
Status Status;
DateTime? ScheduledCall;
}
class CustomerAnalysisModel {
int Id;
string Name;
string Email;
string PhoneNumber;
List<StatusChange> StatusChanges;
DateTime? ScheduledCall;
}
Good Domain Model
• Not too much
• Not too less
• Sweet spot
• The information we need
• The information we might need
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
Event Storage
Entity Id + New events
Entity IdEvent1,
Event2,
Event3,
….
CQRS
Command Query Responsibility Segregation
• 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
Bounded Contexts
Domain Types
• Core Domain
• Subdomain
• Generic Subdomain
Plexop
Campaign Management
(Core Domain)
Creative Catalog
(Subdomain)
User Management
(Generic Subdomain)
Billing
(Generic Subdomain)
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 :)
Questions?
http://il.linkedin.com/in/vladikkhononov
http://twitter.com/vladikk
http://vladikk.com
Introduction to Event Sourcing and CQRS (IASA-IL)
Introduction to Event Sourcing and CQRS (IASA-IL)

More Related Content

Viewers also liked

Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkTaming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Joseph Yoder
 
July 2013 Talk, What Industry Needs from Architecture Description Languages
July 2013 Talk, What Industry Needs from Architecture Description LanguagesJuly 2013 Talk, What Industry Needs from Architecture Description Languages
July 2013 Talk, What Industry Needs from Architecture Description Languages
grossd18
 
Hierarchical Composable Optimization of Web Pages
Hierarchical Composable Optimization of Web PagesHierarchical Composable Optimization of Web Pages
Hierarchical Composable Optimization of Web Pages
Hayim Makabee
 
An Event-Driven Approach for the Separation of Concerns
An Event-Driven Approach for the Separation of ConcernsAn Event-Driven Approach for the Separation of Concerns
An Event-Driven Approach for the Separation of Concerns
Hayim Makabee
 
Software Quality Attributes
Software Quality AttributesSoftware Quality Attributes
Software Quality Attributes
Hayim Makabee
 

Viewers also liked (20)

Antifragile Software Design
Antifragile Software DesignAntifragile Software Design
Antifragile Software Design
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design Patterns
 
To document or not to document? An exploratory study on developers' motivatio...
To document or not to document? An exploratory study on developers' motivatio...To document or not to document? An exploratory study on developers' motivatio...
To document or not to document? An exploratory study on developers' motivatio...
 
Aliyah: Looking for a hi-tech job in Israel
Aliyah: Looking for a hi-tech job in IsraelAliyah: Looking for a hi-tech job in Israel
Aliyah: Looking for a hi-tech job in Israel
 
Agile archiecture iltam 2014
Agile archiecture   iltam 2014Agile archiecture   iltam 2014
Agile archiecture iltam 2014
 
Watch-It-Next: A Contextual TV Recommendation System
Watch-It-Next: A Contextual TV Recommendation SystemWatch-It-Next: A Contextual TV Recommendation System
Watch-It-Next: A Contextual TV Recommendation System
 
Reducing Technical Debt
Reducing Technical DebtReducing Technical Debt
Reducing Technical Debt
 
The Role of the Software Architect (short version)
The Role of the Software Architect (short version)The Role of the Software Architect (short version)
The Role of the Software Architect (short version)
 
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkTaming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
 
ADUF - Adaptable Design Up Front
ADUF -  Adaptable Design Up FrontADUF -  Adaptable Design Up Front
ADUF - Adaptable Design Up Front
 
Adaptable Designs for Agile Software Development
Adaptable Designs for Agile  Software DevelopmentAdaptable Designs for Agile  Software Development
Adaptable Designs for Agile Software Development
 
Introduction to event practice 1
Introduction to event practice 1Introduction to event practice 1
Introduction to event practice 1
 
July 2013 Talk, What Industry Needs from Architecture Description Languages
July 2013 Talk, What Industry Needs from Architecture Description LanguagesJuly 2013 Talk, What Industry Needs from Architecture Description Languages
July 2013 Talk, What Industry Needs from Architecture Description Languages
 
Hierarchical Composable Optimization of Web Pages
Hierarchical Composable Optimization of Web PagesHierarchical Composable Optimization of Web Pages
Hierarchical Composable Optimization of Web Pages
 
An Event-Driven Approach for the Separation of Concerns
An Event-Driven Approach for the Separation of ConcernsAn Event-Driven Approach for the Separation of Concerns
An Event-Driven Approach for the Separation of Concerns
 
Adaptive Object Model - IASA IL Meeting on Software Evolution (3/2014)
Adaptive Object Model - IASA IL Meeting on Software Evolution  (3/2014)Adaptive Object Model - IASA IL Meeting on Software Evolution  (3/2014)
Adaptive Object Model - IASA IL Meeting on Software Evolution (3/2014)
 
Designing with tests
Designing with testsDesigning with tests
Designing with tests
 
Extracting Quality Scenarios from Functional Scenarios
Extracting Quality Scenarios from Functional ScenariosExtracting Quality Scenarios from Functional Scenarios
Extracting Quality Scenarios from Functional Scenarios
 
The five expertise of a software architect
The five expertise of a software architectThe five expertise of a software architect
The five expertise of a software architect
 
Software Quality Attributes
Software Quality AttributesSoftware Quality Attributes
Software Quality Attributes
 

Similar to Introduction to Event Sourcing and CQRS (IASA-IL)

Intro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite AppsIntro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite Apps
dreamforce2006
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
Salesforce Developers
 
Group Project 650 Report2
Group Project 650 Report2Group Project 650 Report2
Group Project 650 Report2
Yazeed Alkarzai
 

Similar to Introduction to Event Sourcing and CQRS (IASA-IL) (20)

Introduction to Event Sourcing and CQRS
Introduction to Event Sourcing and CQRSIntroduction to Event Sourcing and CQRS
Introduction to Event Sourcing and CQRS
 
Apex Design Patterns
Apex Design PatternsApex Design Patterns
Apex Design Patterns
 
Service Architecture patterns
Service Architecture patternsService Architecture patterns
Service Architecture patterns
 
Apex Design Patterns
Apex Design PatternsApex Design Patterns
Apex Design Patterns
 
Intro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite AppsIntro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite Apps
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
 
Detroit ELEVATE Track 2
Detroit ELEVATE Track 2Detroit ELEVATE Track 2
Detroit ELEVATE Track 2
 
SA Chapter 10
SA Chapter 10SA Chapter 10
SA Chapter 10
 
Programming Building Blocks for Admins
Programming Building Blocks for Admins Programming Building Blocks for Admins
Programming Building Blocks for Admins
 
Atl elevate programmatic developer slides
Atl elevate programmatic developer slidesAtl elevate programmatic developer slides
Atl elevate programmatic developer slides
 
Df12 Performance Tuning
Df12 Performance TuningDf12 Performance Tuning
Df12 Performance Tuning
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
 
Greg Lewis SQL Portfolio
Greg Lewis SQL PortfolioGreg Lewis SQL Portfolio
Greg Lewis SQL Portfolio
 
Lerman Vvs14 Ef Tips And Tricks
Lerman Vvs14  Ef Tips And TricksLerman Vvs14  Ef Tips And Tricks
Lerman Vvs14 Ef Tips And Tricks
 
Linq
LinqLinq
Linq
 
Marcus Matthews
Marcus MatthewsMarcus Matthews
Marcus Matthews
 
Group Project 650 Report2
Group Project 650 Report2Group Project 650 Report2
Group Project 650 Report2
 
Job portal
Job portalJob portal
Job portal
 
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
 
Service Provider
Service Provider Service Provider
Service Provider
 

More from Vladik Khononov

Introduction to CQRS and DDDD
Introduction to CQRS and DDDDIntroduction to CQRS and DDDD
Introduction to CQRS and DDDD
Vladik Khononov
 
Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the Hood
Vladik Khononov
 

More from Vladik Khononov (6)

7 Years of DDD: Tackling Complexity in Marketing Systems (DDD Europe 2018)
7 Years of DDD: Tackling Complexity in Marketing Systems (DDD Europe 2018)7 Years of DDD: Tackling Complexity in Marketing Systems (DDD Europe 2018)
7 Years of DDD: Tackling Complexity in Marketing Systems (DDD Europe 2018)
 
How to Tame TDD - ISTA 2017
How to Tame TDD - ISTA 2017How to Tame TDD - ISTA 2017
How to Tame TDD - ISTA 2017
 
ISTA 2016: Event Sourcing
ISTA 2016: Event SourcingISTA 2016: Event Sourcing
ISTA 2016: Event Sourcing
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction to CQRS and DDDD
Introduction to CQRS and DDDDIntroduction to CQRS and DDDD
Introduction to CQRS and DDDD
 
Internal Project: Under the Hood
Internal Project: Under the HoodInternal Project: Under the Hood
Internal Project: Under the Hood
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Introduction to Event Sourcing and CQRS (IASA-IL)

  • 1. Introduction to Event Sourcing … and CQRS
  • 2. Vladik Khononov Chief Architect at Internovus http://il.linkedin.com/in/vladikkhononov/ vladikk vladikkhttp://vladikk.com
  • 8.
  • 10. – www.dictionary.com Model: A simplified representation of a system or phenomenon. – www.wikipedia.org ‫:מודל‬ ‫ייצוג‬‫תאורטי‬‫של‬‫מערכת‬‫מורכבת‬ , ‫שמטרתו‬ ‫לחקות‬‫את‬‫המערכת‬‫בהיבטים‬‫מהותיים‬ . ‫המודל‬‫אינו‬ ‫מתאר‬‫כל‬‫תופעה‬‫במערכת‬ , ‫אלא‬‫מתייחס‬‫להיבטים‬ ‫מוגדרים‬‫ומצומצמים‬‫שלה‬ . ‫המודל‬‫מבוסס‬‫על‬‫קירוב‬‫של‬ ‫המציאות‬‫בדרך‬‫של‬‫הפשטה‬ , ‫איחוד‬‫ישויות‬‫והתעלמות‬ ‫מגורמים‬‫שהשפעתם‬‫אינה‬‫מהותית‬ .
  • 13. Good Domain Model • Not too much • Not too less • Sweet spot • The information we need • The information we will need
  • 14. Why domain models fail • We absolutely suck at predicting the future • No single model can suit all the use cases • Model transformations are painful
  • 16. • 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)
  • 17. • 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
  • 18. 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) )
  • 19. • 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
  • 20. • 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
  • 21. • Analysts should be able to review status changes history for each customer - change date and the new status
  • 22. 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) )
  • 23. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 04-2342343 New
  • 24. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 04-2342343 CallLater 27/10/2014
  • 25. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 CallLater 27/10/2014
  • 26. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 Converted
  • 27. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 Converted
  • 28. Event Sourcing Capture all changes to an application state as a sequence of events ~ Martin Fowler
  • 29.
  • 30. 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; }
  • 31. class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List<IEvent> Events; … void Apply(StatusChanged e) { Status = e.NewStatus; Events.Add(e); } …. …. }
  • 32. class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List<IEvent> Events; … … void Apply(FutureCallScheduled e) { ScheduledCall = e.ScheduledCallTime; Events.Add(e); } … }
  • 33. class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List<IEvent> Events; … … … void Apply(ContactDetailsWereUpdated e) { Name = e.NewName; Email = e.NewEmail; PhoneNumber = e.NewPhoneNumber; Events.Add(e); } }
  • 34. Id Name Email Phone number Status Scheduled Call 10 John john@gmail.com 08-9876653 Converted
  • 35. 1. new StatusChanged(Status.CallLater) 2. new FutureCallScheduled(’27/10/2014’) 3. new ContactDetailsWereUpdated( NewPhoneNumber=’08-9876653’ ) 4. new StatusChanged(Status.Converted)
  • 36. Event Storage Entity Id + New events Entity IdEvent1, Event2, Event3, ….
  • 37. class CustomerSearchModel { int Id; List<string> Names; List<string> Emails; List<string> PhoneNumbers; Status Status; DateTime? ScheduledCall; … … … void Apply(ContactDetailsWereUpdated e) { Names.Add(e.NewName); Emails.Add(e.NewEmail); PhoneNumbers.Add(e.NewPhoneNumber); } }
  • 38. class CustomerAnalysisModel { int Id; string Name; string Email; string PhoneNumber; List<StatusChange> StatusChanges; DateTime? ScheduledCall; … void Apply(StatusChanged e) { StatusChanges.Add( new StatusChange(e.CreatedOn, e.NewStatus) ); } …. …. }
  • 39. class Customer { int Id; string Name; string Email; string PhoneNumber; Status Status; DateTime? ScheduledCall; List<IEvent> Events; }
  • 40. class CustomerSearchModel { int Id; List<string> Names; List<string> Emails; List<string> PhoneNumbers; Status Status; DateTime? ScheduledCall; }
  • 41. class CustomerAnalysisModel { int Id; string Name; string Email; string PhoneNumber; List<StatusChange> StatusChanges; DateTime? ScheduledCall; }
  • 42. Good Domain Model • Not too much • Not too less • Sweet spot • The information we need • The information we might need
  • 43. 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
  • 44.
  • 45. Event Storage Entity Id + New events Entity IdEvent1, Event2, Event3, ….
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52. • 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
  • 54. Domain Types • Core Domain • Subdomain • Generic Subdomain
  • 56. Campaign Management (Core Domain) Creative Catalog (Subdomain) User Management (Generic Subdomain) Billing (Generic Subdomain)
  • 57. 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 :)