26

@despos @andysal74

Embed Size (px)

Citation preview

Applying CQRS and Event Sourcing in .NETDEV-B313Dino EspositoAndrea Saltarello

Breakout Sessions (session codes and titles)

Related content

B211 :: Domain-Driven Design Patterns in .NET

B411 :: EF Model Partitioning in DDD Bounded Contexts

http://naa4e.codeplex.com http://facebook.com/naa4e

@despos @andysal74

CQRSRead stackCommand stackRole of Events in the command stack

Event SourcingPlain and simpleFull-fledged approach

What we’re really covering

Foundation of CQRS

The big issue leading to CQ(R)SA single model caring about all aspects of the domain is hard.

Command/Query Separation (cit. Bertrand Meyer – 1980s)

Query> Returns data> Doesn’t alter state

Command> Alter state > Doesn’t return data

Presentation layer

Application layer

Infrastructure layer

Domain Model

Domain layer

Presentation layer

Infrastructure layer

CQRS

Queries

Data access

Commands

Application+

Domain

CQRS Impact on Complexity

We faced a lot of complexity in modeling We thought it was inherent domain

complexity

That more likely resulted from the Cartesian product of queries and commands

O(C x Q) vs. O(C + Q)

Flavors of CQRS Regular

• Transaction Script in the Command stack• Query stack (LET or SQL)

Premium• Domain Model in the Command stack• Ad-hoc read model

Deluxe• Domain Model and events (sourcing) in the Command

stack• Ad-hoc read model (with snapshots) …

CQRS: Regular

Transaction Script• Task-based, end-to-end use-case implementation • One core “transactional” method per user action• Not strictly model based, “it-just-works” approach• Money-back guarantee!

Two-layer query stack• Just queries: LINQ-to-Entities, ADO.NET• No models; just DTOs

Why making your life harder than it actually is?

Demo

MERP (regular)

Let’s Have LET Defined

Layered Expression Trees It’s an idiom Make queries speak the ubiquitous language Composition of IQueryable extension methods

• IQueryable up to the application layer DSL-like expressivity

LET in actionAs a business unit

manager, I want to

collect credits for all

outgoing invoices still

unpaid Database

How to use ubiquitous language while querying

.IssuedInvoices

.PerBusinessUnit(businessUnitId).Unpaid()

.Select(i => new {

});

InvoiceNumber = i.Number, CustomerId = i.Customer.Id

Demo

LET (operators)

CQRS: Premium

Domain Model in the Command stack• Domain Model focused on behavior that alters state

Ad-hoc storage for queries in the Query stack• Table-per-view strategy• Query through LET

C

Q

CQRS: Deluxe

Domain Model in the Command stack• Domain Model focused on behavior that alters

system ‘s state• Events in the building of tasks• Optionally, events as the primary data source

Ad-hoc storage for queries in the Query stack• Table-per-view strategy• Query through LET• Data snapshots and replay of events

CQRS Deluxe in action

1.Command takes effect 2.System’s state changes3.Changes notified through events to

registered subscribers (aka, handlers)• Workflow managers (aka, sagas)• Denormalizers (used to generate ad-hoc

views)

Application sends a command to the system

Typically: events are notified and commands executed via a mediator (aka, bus)

Demo

Mini-ERP (Command stack)http://naa4e.codeplex.com

Application

Layer

State DB

Event store

Read stack

Domain Layer

Ad-hoc DBHandlers

Command Event Data

Handlers

Model ServicesBUS

Build or buy?SQL XXX MongoDB RavenDB

• Everyone’s skills• Ecosystem of tools• On-Premise & cloud

• Free• Linux and Windows• On-Premise & cloud• Schemaless

• LINQ support• TX+DTC support• Schemaless• On-Premise & cloud

MSMQ NEventStore NServiceBus

• Lots of code • Free• FOSS• DDD/CQRS/ES full

stack

• Advanced config• Advanced features

Breakout Sessions (session codes and titles)

Related content

B211 :: Domain-Driven Design Patterns in .NET

B411 :: EF Model Partitioning in DDD Bounded Contexts

http://naa4e.codeplex.com http://facebook.com/naa4e

@despos @andysal74

http://www.visualstudio.com

http://blogs.msdn.com/b/developer-tools/

http://msdn.microsoft.com/vstudio

DEV Track Resources

visualstudio

@visualstudio

visualstudio

Resources

Learning

Microsoft Certification & Training Resources

www.microsoft.com/learning

TechNet

Resources for IT Professionals

http://microsoft.com/technet

Sessions on Demand

http://channel9.msdn.com/Events/TechEd

Developer Network

http://developer.microsoft.com

Please Complete An Evaluation FormYour input is important!TechEd Schedule Builder CommNet station or PC

TechEd Mobile appPhone or Tablet

QR code

Evaluate this session

© 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.