Applying DDD and CQRS can not only make the resulting design of our system simpler and more effective but, freeing us from the burdenof the “one model fits all” approach, also allows architects to adopt different strategies when it comes to business logic modeling. Though lot has been written about DDD and CQRS, missing working code publicly available seems to be the elephant in the room: in this talk, we’ll find out how to implement the “Command side of the Force” by means of a proper Domain Model and getting to the point of switching from an entity based modeling to an event based one.
A Secure and Reliable Document Management System is Essential.docx
Idiomatic Domain Driven Design: implementing CQRS
1.
2. Idiomatic Domain Driven Design:
implementing CQRS
Andrea Saltarello
CEO @ managed/designs
andysal@gmail.com
http://twitter.com/andysal74
http://slideshare.net/andysal
3. Who I am
1. CEO @ Managed Designs, striving to discover the
perfect «sustainable software development process»
• Customer has to be satisfied and pay
• Supplier has to have a reasonable markup
• Team members should be satisfied with their work
2. Founder and leader of UGIdotNET (first Italian .NET
User Group): I need to share experiences peer to peer
3. (Co)Author (together with Dino) of .NET: Architecting
Applications for the Enterprise, by Microsoft Press
4. Basically, a software developer
4. What’s DDD?
Domain Driven Design:
• Is not a methodology
• Is not an architecture
• Is not a silver bullet
• Is an approach aimed to tackle the complexity of software
development; its main assets are:
–Bounded Context
–Ubiquitous Language
–Aggregates
–(Events)
5. Ubiquitous Language
It’s the language spoken by all the people that
work on a project; the UL spans from the
documentation to the source code.
Basically, technicians should adopt the language
spoken by the business in order to reduce
friction
6. Bounded Context
A Bounded Context is a bounduary whithin which a
specific ubiquitous language applies
A system is a composition (Map) of (bounded) contexts
(e.g.: web store, accountability, delivery&shipment …),
speaking one to each other by means of some kind of
API.
Due to the decoupling of contexts, we can use an ad hoc
architecture for every context
7. Aggregates
Aggregates are atomic clusters of “objects” that exist
and interact inside a specific BC: every Aggregate acts
as the “(both) data and behaviour” composition of an
important concept which exists within the BC itself.
• Basically, a “DDD oriented” system does coordinate
the work of aggregates
• The whole set of aggregates existing within a specific
BC is the BC’s Domain Model.
• Should a specific behaviour exceed an Aggregate’s
bonduary, it will be implemented as a (Domain)
Service
• Model + Services == (Domain) Layer
8. Andrea vs. DDD, A.D. 2004
Got it: a system is a composition Map of
Bounded Contexts, each one sporting a Domain
Layer created after an Ubiquitous Language.
Awesome! Super Cool! Now… How the hell can
we translate this in code?
11. Is Domain Model broken?
Domain Model is (almost) ok, but to follow the
“Blue Book style” is pretty hard and requires a
lot of tradeoffs: is there a different way to have
the same approach applied?
12. DDD has twodistinct parts. You always need one and can
sometimes happily ignore the other.
Analytical
Strategic
Valuable to everybody and every project
Blue book’s Domain Model is just one “supporting architecture”,
though the one being originally recommended
13. Command Query Responsibility Segregation
A single model cannot be appropriate for
reporting, searching and transactional
behaviours
[Greg Young]
15. CQRS in a nutshell
«Doing CQRS» means separating the stack that reads
data (query) from the one that can alter the system’s
state (command), each one having its own ad hoc
«model»
All the actions that the system is capable of are
expressed as Commands. To sum it up:
1. A user An actor ask the system to execute a
command
2. Command execution alters the system’s state
3. The read model is used in order to query for
(updated) data
16. What’s a «MODEL», anyway?
We’re not trying to have a model which
faithfully represents the world, but one that
works within a bounded context
.Net DeveloperDays 2014
17. Mercator projection (1569)
.Net DeveloperDays 2014
That’s the world map we’re used to and… It’s wrong, but it
actually works (from a certain point of view)
19. The Query side of the Force
The «Read Model» can be served in different flavours,
such as:
• Plain SQL
• (Micro) O/RM
• Services (xml, json, DTOs, …)
• LET (Layered Expression Trees)
20. Layered Expression Trees (LET idiom)
As a business unit manager, I want to collect credits due to
unpaid outgoing invoices #ubiquitouslanguage #nuffsaid
Database.OutgoingInvoices.
.PerBusinessUnit(businessUnitId)
.ExpiredOnly()
.Select(i => new {InvoiceNumber = i.Number, CustomerId =
i.Customer.Id})
.AsParallel()
.ForAll(i => bus.Send(new
CollectDebtCommand(i.InvoiceNumber, i.CustomerId)));
22. The Command side of the Force
Sorry, non silver bullet available for implementing
commands, too. Common choices:
• Transaction Script
• Event Sourcing (we will talk about this tomorrow)
23. CQRS feat. Transaction Script
Transaction Script
[P of EAA, 110]
Organizes business logic by procedures where each
procedure handles a single request from the
presentation.
Commands are «modeled» as functions, each one
representing one action that the system is capable of
25. Next stop: Events. See you tomorrow
It really became clear to me in the last couple of years
that we need a new building block and that is the
Domain Event.
[Eric Evans]
An event is something that has happened in the past.
[Greg Young]
A domain event … captures the memory of something
interesting which affects the domain
[Martin Fowler]
26. Bibliography
[DDD] Domain Driven Design, Eric Evans , Addison-Wesley
[NAAE] Microsoft .NET: Architecting Applications for the
Enterprise (2° ed.), Andrea Saltarello & Dino Esposito,
Microsoft Press
[MERP] Merp, https://naa4e.codeplex.com/