35
CQRS Eine Einführung .Net Online Usergroup 10.10.2011 Presenter: Dennis Traub Speaker, Author, Software Developer since 1991 Currently at ProAvia GmbH, Stuttgart Twitter: @DTraub

CQRS - Eine Einführung - NOUG 2011

Embed Size (px)

Citation preview

Page 1: CQRS - Eine Einführung - NOUG 2011

CQRS – Eine Einführung

.Net Online Usergroup – 10.10.2011

Presenter: Dennis Traub

Speaker, Author, Software Developer since 1991 Currently at ProAvia GmbH, Stuttgart

Twitter: @DTraub

Page 2: CQRS - Eine Einführung - NOUG 2011

What CQRS is NOT

Page 3: CQRS - Eine Einführung - NOUG 2011
Page 4: CQRS - Eine Einführung - NOUG 2011

DDD revisited

Usual Patterns

▫ Entities

▫ Value Objects

▫ Repositories

Page 5: CQRS - Eine Einführung - NOUG 2011

DDD revisited

Some Important Patterns

▫ Ubiquitous Language

▫ Bounded Contexts

▫ Core Domain

▫ Domain Experts

Page 6: CQRS - Eine Einführung - NOUG 2011

DDD Revisited

An Example: Competitive advantage through an efficient online claims procedure

Page 7: CQRS - Eine Einführung - NOUG 2011

DDD revisited

DO use DDD:

▫ Where we get competitive advantage

Page 8: CQRS - Eine Einführung - NOUG 2011

DDD revisited

DO use DDD:

▫ Where we get competitive advantage

DON‘T use DDD

▫ When there‘s no value in formalizing the problem

Page 9: CQRS - Eine Einführung - NOUG 2011

DDD revisited

• The point of DDD is not Software Analysis

Page 10: CQRS - Eine Einführung - NOUG 2011

DDD revisited

• The point of DDD is not Software Analysis

• It‘s Business Analysis

Page 11: CQRS - Eine Einführung - NOUG 2011

„Best-Practice“ Architecture

Page 12: CQRS - Eine Einführung - NOUG 2011

„Best-Practice“ Architecture

Page 13: CQRS - Eine Einführung - NOUG 2011

„Best-Practice“ Architecture

• Pros:

▫ Easy to build

▫ Accepted by management

▫ Well known

Page 14: CQRS - Eine Einführung - NOUG 2011

„Best-Practice“ Architecture

• Cons:

▫ Limited scalability

▫ Loss of intent

▫ Lazy Loading

▫ Data Projection

▫ Pain

▫ And: If Domain is only CRUD, where does Business Logic live?

Page 15: CQRS - Eine Einführung - NOUG 2011

Domain Model Responsibilities

• Business Logic • Validation • Structure • Associations • Scopes, Filters • Aggregation • Presentation • Persistence • Lazy / Eager Loading • …

Page 16: CQRS - Eine Einführung - NOUG 2011

The S in SOLID

Page 17: CQRS - Eine Einführung - NOUG 2011
Page 18: CQRS - Eine Einführung - NOUG 2011

Read vs. Write

• Reads and Writes are completely different from eachother

• Why do we try to build the One Model to Rule Them All?

• Sometimes it‘s cheaper to do two things than dealing with the trade-offs

Page 19: CQRS - Eine Einführung - NOUG 2011

Command-Query Separation

• Bertrand Meyer:

▫ Every method should either be a command that performs an action, or a query that returns data to the caller

• In other words:

▫ Asking a question should not change the answer.

Page 20: CQRS - Eine Einführung - NOUG 2011

CQS on an Architectural Level

Data Storage

Commands Queries

Project List

Project Details

Project

Page 21: CQRS - Eine Einführung - NOUG 2011

Basic CQRS Architecture

Data Storage

Thin Read Layer ORM

Domain Model

Command Handlers

Client

Commands Queries

Page 22: CQRS - Eine Einführung - NOUG 2011

The Parts: Thin Read Layer

• Concerns:

▫ Filtering, Scope

▫ Data Presentation

• Attributes

▫ Data-Oriented

▫ Indexable

▫ Can be Denormalized for fast access

▫ Only Structure

Page 23: CQRS - Eine Einführung - NOUG 2011

The Parts: Domain Model

• Concerns: ▫ Validation ▫ Business Logic

• Attributes: ▫ Normalized ▫ Object Oriented ▫ Transactional ▫ Persistence Ignorant ▫ Only Behaviour

Page 24: CQRS - Eine Einführung - NOUG 2011

The Parts: Task-Based UI

Page 25: CQRS - Eine Einführung - NOUG 2011

The Parts: Task-Based UI

Page 26: CQRS - Eine Einführung - NOUG 2011

The Parts: Task-Based UI

Page 27: CQRS - Eine Einführung - NOUG 2011

The Parts: Commands

Refactoring: Application Service Calls to Objects

Commands:

▫ Are Serializable

▫ Can be enveloped

▫ Can be intercepted

▫ Can be enriched

▫ Communicate intent

Page 28: CQRS - Eine Einführung - NOUG 2011

The Parts: Command Handlers

Refactoring: Application Service to Command Handler

Command Handlers:

▫ Unify interface to all application services

▫ Can be wrapped (e.g. Transaction, Authorization, Logging, etc.)

▫ Can be enveloped (e.g. RESTful Envelope)

▫ Rule of Thumb: One Command Handler per Use Case

Page 29: CQRS - Eine Einführung - NOUG 2011

This is CQRS! Complicated?

Data Storage

Thin Read Layer ORM

Domain Model

Command Handlers

Client

Commands Queries

Page 30: CQRS - Eine Einführung - NOUG 2011

But what about Eventual Consistency?

Page 31: CQRS - Eine Einführung - NOUG 2011

Source: www.kinoweb.de

The First Rule

Page 32: CQRS - Eine Einführung - NOUG 2011

CQRS can be perfectly consistent!

Data Storage

Thin Read Layer ORM

Domain Model

Command Handlers

Client

Commands Queries

Page 33: CQRS - Eine Einführung - NOUG 2011

Advanced topics for another day

• Events

• Sagas

• Event Log

• Messaging

• Service Busses

• NoSQL

• Event Store

Page 34: CQRS - Eine Einführung - NOUG 2011

Thanks!

Dennis Traub @DTraub

Page 35: CQRS - Eine Einführung - NOUG 2011

Resources

• cqrsinfo.com

• groups.google.com/group/dddcqrs

• distributedpodcast.com