Top Banner
26
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: @despos @andysal74.
Page 2: @despos @andysal74.

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

Page 3: @despos @andysal74.

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

Page 4: @despos @andysal74.

CQRSRead stackCommand stackRole of Events in the command stack

Event SourcingPlain and simpleFull-fledged approach

What we’re really covering

Page 5: @despos @andysal74.

Foundation of CQRS

Page 6: @despos @andysal74.

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

Page 7: @despos @andysal74.

Presentation layer

Application layer

Infrastructure layer

Domain Model

Domain layer

Presentation layer

Infrastructure layer

CQRS

Queries

Data access

Commands

Application+

Domain

Page 8: @despos @andysal74.

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)

Page 9: @despos @andysal74.

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

Page 10: @despos @andysal74.

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?

Page 11: @despos @andysal74.

Demo

MERP (regular)

Page 12: @despos @andysal74.

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

Page 13: @despos @andysal74.

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

Page 14: @despos @andysal74.

Demo

LET (operators)

Page 15: @despos @andysal74.

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

Page 16: @despos @andysal74.

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

Page 17: @despos @andysal74.

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)

Page 18: @despos @andysal74.

Demo

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

Page 19: @despos @andysal74.

Application

Layer

State DB

Event store

Read stack

Domain Layer

Ad-hoc DBHandlers

Command Event Data

Handlers

Model ServicesBUS

Page 20: @despos @andysal74.

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

Page 21: @despos @andysal74.

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

Page 22: @despos @andysal74.

http://www.visualstudio.com

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

http://msdn.microsoft.com/vstudio

DEV Track Resources

visualstudio

@visualstudio

visualstudio

Page 23: @despos @andysal74.

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

Page 24: @despos @andysal74.

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

TechEd Mobile appPhone or Tablet

QR code

Page 25: @despos @andysal74.

Evaluate this session

Page 26: @despos @andysal74.

© 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.