Top Banner
27

Idiomatic Domain Driven Design: implementing CQRS

Jun 20, 2015

Download

Software

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Idiomatic Domain Driven Design: implementing CQRS
Page 2: Idiomatic Domain Driven Design: implementing CQRS

Idiomatic Domain Driven Design: implementing CQRS

Andrea SaltarelloCEO @ managed/designs

[email protected]

http://twitter.com/andysal74

http://slideshare.net/andysal

Page 3: Idiomatic Domain Driven Design: implementing CQRS

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

Page 4: Idiomatic Domain Driven Design: implementing CQRS

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)

Page 5: Idiomatic Domain Driven Design: implementing CQRS

Ubiquitous Language

It’s the language spoken by all the people thatwork on a project; the UL spans from the documentation to the source code.

Basically, technicians should adopt the languagespoken by the business in order to reduce friction

Page 6: Idiomatic Domain Driven Design: implementing CQRS

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

Page 7: Idiomatic Domain Driven Design: implementing CQRS

Aggregates

Aggregates are atomic clusters of “objects” that existand interact inside a specific BC: every Aggregate actsas 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 specificBC is the BC’s Domain Model.

• Should a specific behaviour exceed an Aggregate’sbonduary, it will be implemented as a (Domain) Service

• Model + Services == (Domain) Layer

Page 8: Idiomatic Domain Driven Design: implementing CQRS

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?

Page 9: Idiomatic Domain Driven Design: implementing CQRS

2004: a Domain (Model) Odissey

Page 10: Idiomatic Domain Driven Design: implementing CQRS

DEMO

Domain Model (Voucher, NSK)

Page 11: Idiomatic Domain Driven Design: implementing CQRS

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 havethe same approach applied?

Page 12: Idiomatic Domain Driven Design: implementing CQRS

DDD has two distinct 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

Page 13: Idiomatic Domain Driven Design: implementing CQRS

Command Query Responsibility Segregation

A single model cannot be appropriate for reporting, searching and transactional

behaviours

[Greg Young]

Page 14: Idiomatic Domain Driven Design: implementing CQRS

CQRS at a glance

Query Command

Page 15: Idiomatic Domain Driven Design: implementing CQRS

CQRS in a nutshell

«Doing CQRS» means separating the stack that readsdata (query) from the one that can alter the system’sstate (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

Page 16: Idiomatic Domain Driven Design: implementing CQRS

What’s a «MODEL», anyway?

We’re not trying to have a model whichfaithfully represents the world, but one thatworks within a bounded context

.Net DeveloperDays 2014

Page 17: Idiomatic Domain Driven Design: implementing CQRS

Mercator projection (1569)

.Net DeveloperDays 2014

That’s the world map we’re used to and… It’s wrong, but itactually works (from a certain point of view)

Page 18: Idiomatic Domain Driven Design: implementing CQRS

Galls-Peters projection (1885 ⊕ 1973)

.Net DeveloperDays 2014

Page 19: Idiomatic Domain Driven Design: implementing CQRS

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)

Page 20: Idiomatic Domain Driven Design: implementing CQRS

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)));

Page 21: Idiomatic Domain Driven Design: implementing CQRS

DEMO

Layered Expression Trees

Page 22: Idiomatic Domain Driven Design: implementing CQRS

The Command side of the Force

Sorry, non silver bullet available for implementingcommands, too. Common choices:

• Transaction Script

• Event Sourcing (we will talk about this tomorrow)

Page 23: Idiomatic Domain Driven Design: implementing CQRS

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 onerepresenting one action that the system is capable of

Page 24: Idiomatic Domain Driven Design: implementing CQRS

DEMO

Transaction Script feat. CQRS

Page 25: Idiomatic Domain Driven Design: implementing CQRS

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]

Page 26: Idiomatic Domain Driven Design: implementing CQRS

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/

Page 27: Idiomatic Domain Driven Design: implementing CQRS