B211 :: Domain-Driven Design Patterns in .NET http://naa4e.codeplex.com @despos @andysal74 http://facebook.com/naa4e (R) A single model caring about all aspects of the domain is hard. Command/Query Separation (cit.

Download Report

Transcript B211 :: Domain-Driven Design Patterns in .NET http://naa4e.codeplex.com @despos @andysal74 http://facebook.com/naa4e (R) A single model caring about all aspects of the domain is hard. Command/Query Separation (cit.

B211 :: Domain-Driven Design Patterns in .NET
http://naa4e.codeplex.com
@despos
@andysal74
http://facebook.com/naa4e
(R)
A single model caring about all aspects of the domain is hard.
Command/Query Separation
(cit. Bertrand Meyer – 1980s)
Command
Query
> Alter state
> Doesn’t return data
> Returns data
> Doesn’t alter state
CQRS
Domain Model
Presentation layer
Presentation layer
Application layer
Domain layer
Infrastructure layer
Infrastructure layer
Commands
Queries
 We faced a lot of complexity in modeling
 We thought it was inherent domain complexity
 That more likely resulted from the Cartesian product of
queries and commands
O(C x Q) vs. O(C + Q)
 Regular
• Transaction Script in the Command stack
• Query stack (LET or SQL)
 Premium
• Domain Model in the Command stack
• Ad-hoc read model
 Deluxe
• Domain Model and events (sourcing) in the Command stack
• Ad-hoc read model (with snapshots)
…
Regular
 Transaction Script
•
•
•
•
Task-based, end-to-end use-case implementation
One core “transactional” method per user action
Not strictly model based, “it-just-works” approach
Money-back guarantee!
 Two-layer query stack
• Just queries: LINQ-to-Entities, ADO.NET
• No models; just DTOs




Layered Expression Trees
It’s an idiom
Make queries speak the ubiquitous language
Composition of IQueryable extension methods
• IQueryable up to the application layer
 DSL-like expressivity
How to use ubiquitous language while querying
Database
.IssuedInvoices
.PerBusinessUnit(businessUnitId)
.Unpaid()
.Select(i => new {
InvoiceNumber = i.Number,
CustomerId
= i.Customer.Id
});
Premium
 Domain Model in the Command stack
• Domain Model focused on behavior that alters state
C
 Ad-hoc storage for queries in the Query stack
• Table-per-view strategy
• Query through LET
Q
Deluxe
 Domain Model in the Command stack
• Domain Model focused on behavior that alters system ‘s state
• Events in the building of tasks
• Optionally, events as the primary data source
 Ad-hoc storage for queries in the Query stack
• Table-per-view strategy
• Query through LET
• Data snapshots and replay of events
Application sends a command to the system
1. Command takes effect
2. System’s state changes
3. Changes notified through events to registered
subscribers (aka, handlers)
• Workflow managers (aka, sagas)
• Denormalizers (used to generate ad-hoc views)
Typically: events are notified and commands executed via
a mediator (aka, bus)
http://naa4e.codeplex.com
Domain Layer
Application
Layer
Handlers
Read stack
SQL XXX
• Everyone’s skills
• Ecosystem of tools
• On-Premise & cloud
MSMQ
• Lots of code
MongoDB
•
•
•
•
Free
Linux and Windows
On-Premise & cloud
Schemaless
NEventStore
• Free
• FOSS
• DDD/CQRS/ES full
stack
RavenDB
•
•
•
•
LINQ support
TX+DTC support
Schemaless
On-Premise & cloud
NServiceBus
• Advanced config
• Advanced features
B211 :: Domain-Driven Design Patterns in .NET
http://naa4e.codeplex.com
@despos
@andysal74
http://facebook.com/naa4e
http://www.visualstudio.com
http://blogs.msdn.com/b/developer-tools/
http://msdn.microsoft.com/vstudio
http://channel9.msdn.com/Events/TechEd
www.microsoft.com/learning
http://microsoft.com/technet
http://developer.microsoft.com