Introduction to CQRS and DDDD
-
Upload
vladik-khononov -
Category
Technology
-
view
235 -
download
1
description
Transcript of Introduction to CQRS and DDDD
EXORCISM“THE PRACTICE OF EVICTING DEMONS OR OTHER SPIRITUAL ENTITIES FROM A PERSON OR AN AREA THEY ARE BELIEVED TO HAVE POSSESSED”
CQRSDISTRIBUTED DOMAIN DRIVEN DESIGN
Classic ArchitectureClient executes interactors
Interactor modifies data and returns ViewModel
Step 1: CQSCommand/Query segregation
Commands - write data, void
Queries - read data
Step 1: CQSpublic class SubmitAction : Interactor{
public ShowLeadViewModel Execute(LeadActionDTO action){
var lead = ExecuteLeadAction(action); var viewModel = BuildViewModel(lead);
return viewModel;}
}
Step 1: CQSpublic class SubmitAction : Interactor{
public void Execute(LeadActionDTO action){
ExecuteLeadAction(action);}
public ShowLeadViewModel GetLeadInfo(long id){
var lead = GetLead(id); var viewModel = BuildViewModel(lead);
return viewModel;}
}
Step 1: CQSpublic class SubmitAction : Interactor{
public void Execute(LeadActionDTO action){
ExecuteLeadAction(action);}
public ShowLeadViewModel GetLeadInfo(long id){
return GetLeadViewModel(id);}
}
Step 1: CQS
Separated Query/Command logic
Commands are executed asynchronously
Queries are executed though a thin read layer
Normalization: 3NF vs. 1NF
lead_id status_id queue_idcontact_i
dcampaign_id
assigned_to_id
1043 1 2 241734 54 108
1044 3 5 241771 78 109
lead_id status queuecontact_name
campaignassigned_to
1043 New WaitYau
SiongSaraya
[03SEP2012]Alex
Delors
1043Contacte
dConverte
dDimas
WijayaGunggo_
CA [060512]Mona
Barbara
Step 2
Write model: 3NF
Read model: 1NF
Step 2
Step 2: Events
Step 2: Events
Multiple projections
Scalable reads
Decoupled storage mechanism
RDMBS, Document store, Key/Value store, plain files (local or S3), Sharepoint List, Lucene, Cloud Search
No need for NHibernate Profiler licenses
Step 2: Events
No reliable source of truth.
Step 3
Step 3: Events History
Step 3: Events History
Every data change is logged
Reliable source of truth
Read model can be regenerated anytime
Write model can be regenerated anytime
Step 3: Events History
Writes are not scalable.
Step 4
Step 4var lead1 = {
“leadId”: 15423,“customer_id”: “[email protected]”,“events”: [
{ “type”: “lead_created”, “data”: {…}, “date”: “20131216T1242” },
{ “type”: “lead_assigned”, “data”: {…}, “date”: “20131216T1342” },
{ “type”: “activity_created”, “data”: {…}, “date”: “20131216T1432” },
{ “type”: “new_lead_event”, “data”: {…}, “date”: “20131216T1522” },
{ “type”: “activity_created”, “data”: {…}, “date”: “20131216T1432” }
]}
Step 4
Step 4: Event Sourcing
Step 4: Event Sourcing
Infinitely scalable reads
Infinitely scalable writes
Audit data
New business insights form past data
Replay events for debugging and analysis
Retroactive logic changes (=bug fixes)
Lightweight backups
NoSQL
No deadlocks
No DBA
CHEAP!!!!!
CQRS
Commands
Events
Event Store
ViewModels / Projections
Questions?
DDD: Building Blocks
Domain Driven Design
Entity
Aggregate
Repository
DDD: Aggregate
shoppingCart = {“id”: 1234,“items”: [{
{ “product_id”: 123, “amount”: 1, “price”: 10},{ “product_id”: 123, “amount”: 1, “price”: 10}
}]}