SlideShare a Scribd company logo
Andrea Saltarello
@andysal74
CEO @ Managed Designs
http://www.manageddesigns.it
Making workflow implementation
easy with CQRS
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
A workflow consists of an orchestrated and
repeatable pattern of business activity enabled by the
systematic organization of resources into processes that
transform materials, provide services, or process
information.[1]
It can be depicted as a sequence of operations,
declared as work of a person or group,[2] an organization
of staff, or one or more simple or complex mechanisms.
[Wikipedia]
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
A simple workflow: Invoicing
Issue
Payment
Ship
NotifyExpires IsPaid Collect
No
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
A simple workflow: Invoicing
Either a user or a scheduler fires code such as:
public void NotifyCustomersAboutPertainingExpiredInvoices()
{
var expiredInvoices = from i in db.OutgoingInvoices
where SomeClausesPointingToStillUnpaidExpiredInvoices
select i;
foreach(var invoice in expiredInvoices)
{
NotifyExpiredInvoiceToCustomer(invoice);
}
}
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
Enter CQRS
Presentation layer
Application layer
Infrastructure layer
Domain Model
Domain layer
Presentation layer
Infrastructure layer
CQRS
Queries
Data
access
Commands
Application
+
Domain
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
Why CQRS?
We faced a lot of complexity in modeling
We thought it was inherent domain complexity
Long story short: a single model caring about all aspects of the domain is
hard. Enter CQS
Command/Query Separation
(cit. Bertrand Meyer – 1980s)
Query
> Returns data
> Doesn’t alter state
Command
> Alter state
> Doesn’t return data
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
CQRS in action
As a business unit manager, I want to notify
customers about expired invoices
Database
.OutgoingInvoices.
.PerBusinessUnit(businessUnitId)
.ExpiredOnly()
.Select(i => new {InvoiceNumber = i.Number, CustomerId =
i.Customer.Id})
.AsParallel()
.ForAll(i => NotifyCustomer(i.InvoiceNumber, i.CustomerId));
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
The result
Pros:
• Easy to read
• Close to Ubiquitous Language
Cons:
• Synchronous (lower scalability)
• Either a scheduling engine or human interaction is
required
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
Commands: a different strategy
1. Application sends a command to the system
2. Commands are dispatched to workflow managers
(a.k.a. Sagas) which will execute them and then state
success/failure accordingly
3. Responses are notified to interested subscribers (a.k.a.
handlers) such as Denormalizers, which will
(eventually) update the read model’s database
Note: command/responses dispatch will be managed by a
Mediator
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
CQRS in action, part 2
As a business unit manager, I want to notify
customers about expired invoices
Database
.OutgoingInvoices.
.PerBusinessUnit(businessUnitId)
.ExpiredOnly()
.Select(i => new {InvoiceNumber = i.Number, CustomerId = i.Customer.Id})
.AsParallel()
.ForAll(i => mediator.Send(new NotifyPaymentDueCommand(i.InvoiceNumber,
i.CustomerId)));
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
The result, part 2
Pros:
• Easy to read
• Close to Ubiquitous Language
• Asynchronous
Cons:
• Either a scheduling engine or human interaction is
required
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
Queues or buses? That is the question (semi cit. )
Although both categories are viable options and support
common features (e.g.: message durability), a bus is generally
preferable due to advanced capabilities (e.g.: message
scheduling)
Mediator, who art thou?
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
Sagas 1-2-3
In a messaging system, a saga orchestrate a set of
messages. The main benefit of using a saga is that it
allows us to manage the interaction in a stateful manner
(easy to think and reason about) while actually working in a
distributed and asynchronous environment.
[Ayende]
Though Saga are not proper workflow/process manager,
they are effective at pretending to be 
Sagas in action
Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS
More resources:
• “Microsoft .NET: Architecting Applications for the Enterprise”
by Andrea Saltarello and Dino Esposito, Microsoft Press (2014)
• Merp (http://naa4e.codeplex.com)
Links
Thank you

More Related Content

Similar to Making workflow implementation easy with CQRS

lecture13.ppt
lecture13.pptlecture13.ppt
lecture13.ppt
IrfanAkbar35
 
Cloud computing set 3
Cloud computing set 3Cloud computing set 3
Cloud computing set 3
Nicole Valerio
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
Vikash Sharma
 
8 Key Elements to Modern IT Operations Management with a Digital Operations C...
8 Key Elements to Modern IT Operations Management with a Digital Operations C...8 Key Elements to Modern IT Operations Management with a Digital Operations C...
8 Key Elements to Modern IT Operations Management with a Digital Operations C...
OpsRamp
 
Getting Started with Nastel AutoPilot Business Views and Policies - a Tutorial
Getting Started with Nastel AutoPilot Business Views and Policies - a TutorialGetting Started with Nastel AutoPilot Business Views and Policies - a Tutorial
Getting Started with Nastel AutoPilot Business Views and Policies - a Tutorial
Sam Garforth
 
Cbse computer science (c++) class 12 board project bank managment system
Cbse computer science (c++)  class 12 board project  bank managment systemCbse computer science (c++)  class 12 board project  bank managment system
Cbse computer science (c++) class 12 board project bank managment system
pranoy_seenu
 
Tutorial 22 mastering olap reporting drilling through using mdx
Tutorial 22 mastering olap reporting drilling through using mdxTutorial 22 mastering olap reporting drilling through using mdx
Tutorial 22 mastering olap reporting drilling through using mdx
Subandi Wahyudi
 
Proposed pricing model for cloud computing
Proposed pricing model for cloud computingProposed pricing model for cloud computing
Proposed pricing model for cloud computing
Adeel Javaid
 
1.My Presentation.pptx
1.My Presentation.pptx1.My Presentation.pptx
1.My Presentation.pptx
ArslanAliArslanAli
 
Thesis1 3-23
Thesis1 3-23Thesis1 3-23
Thesis1 3-23
Hemant Dhoot
 
Synchronicity of a distributed financial system
Synchronicity of a distributed financial systemSynchronicity of a distributed financial system
Synchronicity of a distributed financial system
DataWorks Summit
 
Smart Services & Smart Clients - How Microservices Change the Way You Build a...
Smart Services & Smart Clients - How Microservices Change the Way You Build a...Smart Services & Smart Clients - How Microservices Change the Way You Build a...
Smart Services & Smart Clients - How Microservices Change the Way You Build a...
Neil Mansilla
 
WF_in_retail_banking_enterprise_systems
WF_in_retail_banking_enterprise_systemsWF_in_retail_banking_enterprise_systems
WF_in_retail_banking_enterprise_systemsOleh Zheleznyak
 
Survey on reliable sla based monitoring for billing scheme in cloud computing
Survey on reliable sla based monitoring for billing scheme in cloud computingSurvey on reliable sla based monitoring for billing scheme in cloud computing
Survey on reliable sla based monitoring for billing scheme in cloud computing
eSAT Journals
 
Scheduling
SchedulingScheduling
Orwell Hortonworks Data Summit
Orwell Hortonworks Data SummitOrwell Hortonworks Data Summit
Orwell Hortonworks Data Summit
Luis Caldeira
 
Sap tr um_dm_meter reading order reversal_v1.0
Sap tr um_dm_meter reading order reversal_v1.0Sap tr um_dm_meter reading order reversal_v1.0
Sap tr um_dm_meter reading order reversal_v1.0
UF Technology
 
Dependency injection - the right way
Dependency injection - the right wayDependency injection - the right way
Dependency injection - the right way
Thibaud Desodt
 
Managed cloud-transition-service-brief
Managed cloud-transition-service-briefManaged cloud-transition-service-brief
Managed cloud-transition-service-briefFirman Indrianto
 

Similar to Making workflow implementation easy with CQRS (20)

lecture13.ppt
lecture13.pptlecture13.ppt
lecture13.ppt
 
Cloud computing set 3
Cloud computing set 3Cloud computing set 3
Cloud computing set 3
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
8 Key Elements to Modern IT Operations Management with a Digital Operations C...
8 Key Elements to Modern IT Operations Management with a Digital Operations C...8 Key Elements to Modern IT Operations Management with a Digital Operations C...
8 Key Elements to Modern IT Operations Management with a Digital Operations C...
 
Getting Started with Nastel AutoPilot Business Views and Policies - a Tutorial
Getting Started with Nastel AutoPilot Business Views and Policies - a TutorialGetting Started with Nastel AutoPilot Business Views and Policies - a Tutorial
Getting Started with Nastel AutoPilot Business Views and Policies - a Tutorial
 
Cbse computer science (c++) class 12 board project bank managment system
Cbse computer science (c++)  class 12 board project  bank managment systemCbse computer science (c++)  class 12 board project  bank managment system
Cbse computer science (c++) class 12 board project bank managment system
 
Tutorial 22 mastering olap reporting drilling through using mdx
Tutorial 22 mastering olap reporting drilling through using mdxTutorial 22 mastering olap reporting drilling through using mdx
Tutorial 22 mastering olap reporting drilling through using mdx
 
Proposed pricing model for cloud computing
Proposed pricing model for cloud computingProposed pricing model for cloud computing
Proposed pricing model for cloud computing
 
1.My Presentation.pptx
1.My Presentation.pptx1.My Presentation.pptx
1.My Presentation.pptx
 
Thesis1 3-23
Thesis1 3-23Thesis1 3-23
Thesis1 3-23
 
Synchronicity of a distributed financial system
Synchronicity of a distributed financial systemSynchronicity of a distributed financial system
Synchronicity of a distributed financial system
 
Smart Services & Smart Clients - How Microservices Change the Way You Build a...
Smart Services & Smart Clients - How Microservices Change the Way You Build a...Smart Services & Smart Clients - How Microservices Change the Way You Build a...
Smart Services & Smart Clients - How Microservices Change the Way You Build a...
 
WF_in_retail_banking_enterprise_systems
WF_in_retail_banking_enterprise_systemsWF_in_retail_banking_enterprise_systems
WF_in_retail_banking_enterprise_systems
 
Survey on reliable sla based monitoring for billing scheme in cloud computing
Survey on reliable sla based monitoring for billing scheme in cloud computingSurvey on reliable sla based monitoring for billing scheme in cloud computing
Survey on reliable sla based monitoring for billing scheme in cloud computing
 
Scheduling
SchedulingScheduling
Scheduling
 
Orwell Hortonworks Data Summit
Orwell Hortonworks Data SummitOrwell Hortonworks Data Summit
Orwell Hortonworks Data Summit
 
Sap tr um_dm_meter reading order reversal_v1.0
Sap tr um_dm_meter reading order reversal_v1.0Sap tr um_dm_meter reading order reversal_v1.0
Sap tr um_dm_meter reading order reversal_v1.0
 
Dependency injection - the right way
Dependency injection - the right wayDependency injection - the right way
Dependency injection - the right way
 
Proteus CMMS Overview
Proteus CMMS OverviewProteus CMMS Overview
Proteus CMMS Overview
 
Managed cloud-transition-service-brief
Managed cloud-transition-service-briefManaged cloud-transition-service-brief
Managed cloud-transition-service-brief
 

Recently uploaded

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 

Recently uploaded (20)

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 

Making workflow implementation easy with CQRS

  • 1. Andrea Saltarello @andysal74 CEO @ Managed Designs http://www.manageddesigns.it Making workflow implementation easy with CQRS
  • 2. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS A workflow consists of an orchestrated and repeatable pattern of business activity enabled by the systematic organization of resources into processes that transform materials, provide services, or process information.[1] It can be depicted as a sequence of operations, declared as work of a person or group,[2] an organization of staff, or one or more simple or complex mechanisms. [Wikipedia]
  • 3. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS A simple workflow: Invoicing Issue Payment Ship NotifyExpires IsPaid Collect No
  • 4. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS A simple workflow: Invoicing Either a user or a scheduler fires code such as: public void NotifyCustomersAboutPertainingExpiredInvoices() { var expiredInvoices = from i in db.OutgoingInvoices where SomeClausesPointingToStillUnpaidExpiredInvoices select i; foreach(var invoice in expiredInvoices) { NotifyExpiredInvoiceToCustomer(invoice); } }
  • 5. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS Enter CQRS Presentation layer Application layer Infrastructure layer Domain Model Domain layer Presentation layer Infrastructure layer CQRS Queries Data access Commands Application + Domain
  • 6. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS Why CQRS? We faced a lot of complexity in modeling We thought it was inherent domain complexity Long story short: a single model caring about all aspects of the domain is hard. Enter CQS Command/Query Separation (cit. Bertrand Meyer – 1980s) Query > Returns data > Doesn’t alter state Command > Alter state > Doesn’t return data
  • 7. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS CQRS in action As a business unit manager, I want to notify customers about expired invoices Database .OutgoingInvoices. .PerBusinessUnit(businessUnitId) .ExpiredOnly() .Select(i => new {InvoiceNumber = i.Number, CustomerId = i.Customer.Id}) .AsParallel() .ForAll(i => NotifyCustomer(i.InvoiceNumber, i.CustomerId));
  • 8. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS The result Pros: • Easy to read • Close to Ubiquitous Language Cons: • Synchronous (lower scalability) • Either a scheduling engine or human interaction is required
  • 9. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS Commands: a different strategy 1. Application sends a command to the system 2. Commands are dispatched to workflow managers (a.k.a. Sagas) which will execute them and then state success/failure accordingly 3. Responses are notified to interested subscribers (a.k.a. handlers) such as Denormalizers, which will (eventually) update the read model’s database Note: command/responses dispatch will be managed by a Mediator
  • 10. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS CQRS in action, part 2 As a business unit manager, I want to notify customers about expired invoices Database .OutgoingInvoices. .PerBusinessUnit(businessUnitId) .ExpiredOnly() .Select(i => new {InvoiceNumber = i.Number, CustomerId = i.Customer.Id}) .AsParallel() .ForAll(i => mediator.Send(new NotifyPaymentDueCommand(i.InvoiceNumber, i.CustomerId)));
  • 11. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS The result, part 2 Pros: • Easy to read • Close to Ubiquitous Language • Asynchronous Cons: • Either a scheduling engine or human interaction is required
  • 12. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS Queues or buses? That is the question (semi cit. ) Although both categories are viable options and support common features (e.g.: message durability), a bus is generally preferable due to advanced capabilities (e.g.: message scheduling) Mediator, who art thou?
  • 13. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS Sagas 1-2-3 In a messaging system, a saga orchestrate a set of messages. The main benefit of using a saga is that it allows us to manage the interaction in a stateful manner (easy to think and reason about) while actually working in a distributed and asynchronous environment. [Ayende] Though Saga are not proper workflow/process manager, they are effective at pretending to be 
  • 15. Introduction to ServiceInsight for NServiceBusMaking workflow implementation easy with CQRS More resources: • “Microsoft .NET: Architecting Applications for the Enterprise” by Andrea Saltarello and Dino Esposito, Microsoft Press (2014) • Merp (http://naa4e.codeplex.com) Links