Top Banner
Domain-Driven Design and CQRS Xebia ITR on Command Query Responsibility Separation Sjors Grijpink and Erik Rozendaal
37

Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Jun 23, 2020

Download

Documents

dariahiddleston
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: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Domain-Driven Design and CQRS

Xebia ITR on Command Query Responsibility Separation

Sjors Grijpink and Erik Rozendaal

Page 2: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

DDD

• Eric Evans, Domain-Driven Design, 2004

• Key concepts

• Ubiquitous Language

• Value Object, Entity

• Aggregate

Page 3: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Ubiquitous Language

• Language shared between domain experts and developers

• No need for error-prone translation

• Maps directly to domain implementation

• Implementation should be free of “technical” terms

Page 4: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Value Object

• No conceptual identity

• Describe characteristic of a thing

• Usually immutable

• Examples: Address, Money, ...

Page 5: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Entity

• Something with a unique identity

• Identity does not change when any of its attributes change

• Examples: Customer, Order, ...

Page 6: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Aggregate

• Group of Entities & Value Objects

• One entity within the aggregate is the aggregate root

• All access to the objects inside go through the root entity

• Aggregates are consistency boundaries

Page 7: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Lottery

• Two aggregate roots

• Lottery

• Customer

Lottery

Ticket

Customer

Page 8: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

3 Tier

• Presentation layer is denormalized

• Domain is behavioral

• Transactional database is normalized

Page 9: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

“A single model cannot be appropriate for reporting,

searching, and transactional behaviors.” - Greg Young

Page 10: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR
Page 11: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR
Page 12: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR
Page 13: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR
Page 14: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR
Page 15: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Purchase LotteryTicket

Page 16: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Purchase LotteryTicket

Lottery TicketPurchased

Page 17: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Purchase LotteryTicket

Lottery TicketPurchased

Find Lottery Tickets for Customer

Page 18: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

All state changes are represented by Domain Events

Page 19: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Domain

Page 20: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Reporting

Page 21: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Packages

Domain Presentation Reporting

Commands Events Queries

Page 22: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Packages

Domain Presentation Reporting

Commands Events Queries

Shared

Page 23: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Packages

Domain Presentation Reporting

Commands Events Queries

SharedCQRS

framew

ork

Page 24: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Exercise 1

• Implement “Create Customer”

• Test that customers are listed on the screen

• Test that customers cannot be created with an initial payment of less than 10.00

Page 25: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Exercise 2

• Implement “Purchase Lottery Ticket”

• Ticket should be listed

• Customer’s balance should be updated

• Don’t forget to check the customer’s account balance

Page 26: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits

Page 27: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits• Fully encapsulated domain that only exposes behavior

Page 28: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits• Fully encapsulated domain that only exposes behavior

• Queries do not use the domain model

Page 29: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits• Fully encapsulated domain that only exposes behavior

• Queries do not use the domain model

• No object-relational impedance mismatch

Page 30: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits• Fully encapsulated domain that only exposes behavior

• Queries do not use the domain model

• No object-relational impedance mismatch

• Bullet-proof auditing and historical tracing

Page 31: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits• Fully encapsulated domain that only exposes behavior

• Queries do not use the domain model

• No object-relational impedance mismatch

• Bullet-proof auditing and historical tracing

• Easy integration with external systems

Page 32: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits• Fully encapsulated domain that only exposes behavior

• Queries do not use the domain model

• No object-relational impedance mismatch

• Bullet-proof auditing and historical tracing

• Easy integration with external systems

• Performance and scalability

Page 33: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Benefits• Fully encapsulated domain that only exposes behavior

• Queries do not use the domain model

• No object-relational impedance mismatch

• Bullet-proof auditing and historical tracing

• Easy integration with external systems

• Performance and scalability

• Testability

Page 34: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Bounded Context

Page 35: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Advanced Topics

• Snapshots

• Command-event conflict resolution

• Eventual consistency

• Transaction-aware repository

• ...

Page 36: Domain-Driven Design and CQRS - storage.googleapis.comstorage.googleapis.com › xebia-blog › 1 › 2009 › 12 › Xebia-ITR-CQRS... · Domain-Driven Design and CQRS Xebia ITR

Snapshot

Event Store

4

snapshot

3

2

1

0

Repository

saveload