18
CQRS & Domain Models Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist

DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Embed Size (px)

Citation preview

Page 1: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

CQRS & Domain Models

Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist

Page 2: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

What most architecture looks like

Page 3: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Common Mistakes #1

Assuming that layers == tiers

We can deploy the same components both client-side and server-side

Page 4: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

But where do business rules go?

Here?

Here?

Here?Here?

Here?

Here?

Page 5: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Code Reuse On A Pedestal

Understanding existing code takes longerthan writing new code

Page 6: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Too Much Of A “Good Thing”

Page 7: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

For Example - CQRSQueries

CommandsDB

WSInput

Validation Validation

Rules

QueriesView Model

View ModelUpdater

PublishUI

Page 8: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Common Mistake #2

Assuming that all rules are equally likely to change

Page 9: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Rules We Know How To Handle

In the domain model only:

When a customer cancels an order,if the order was already shipped,charge the customer $$unless customer is {some status}

UIServic

esBL

DAL

DB

Page 10: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Rules We Struggle With

Where to enforce these rules?

- First name less than N chars- Username must be unique

UIServic

esBL

DAL

DBNot Domain Logic!

Page 11: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

The Domain ModelWhen To Use It & When Not To

“If you have complicated and everchanging business rules…”

“If you have simple not-null checks and a couple of sums to calculate, a Transaction Script is a better bet”

-- Martin Fowler, p119 Patterns of Enterprise Application Architecture

Page 12: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Common Mistake #3

Assuming that race conditions affect the domain

Page 13: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Race Conditions

Ship OrderCancel Order

We think this should fail

Page 14: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

What Domain Events Are For

What else needs to happen?

When a customer cancels an order,if the order was already shipped,charge the customer $$unless customer is {some status}

DomainEvents.Raise<InvokeCancellationFee>(f => f.OrderId = msg.OrderId );

Page 15: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Consider The Reverse

Going to ship a cancelled order

Race condition between command & query

“One in a million” failure Let it fail asynchronously

Email back to the user“Sorry, that order doesn’t exist anymore”

Page 16: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Aggregate Mistakes

public class Customer{

public void MakePreferred(){

foreach(Order o in this.UnbilledOrders)

o.Discount(10.Percent);}

}

Page 17: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

In Closing

Avoid the common mistakes:#1 Layers & tiers#2 Rate of rule change#3 Race Conditions

Think about aggregates & bounded contexts

Page 18: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

Thank youUdi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist

www.UdiDahan.com

[email protected]