Implementing Event Sourcing in .NET

16

description

When you use the Event Sourcing pattern in a .NET application, your data source just consists of persisted events. You don’t likely have a classic relational data store; all you store are events, and you store them sequentially as they occur in the domain. As you can guess, persisting events instead of a domain model has a significant impact on the way you organize the back end of the system. In this talk, we just develop a mini-ERP application that works out of distinct command and query stacks and persists just events. We also discuss how to rebuild state from events and see how to manage snapshot in order to speed up OLTP performances. Overall, this session presents a concrete example of an application architecture that for its inherent simplicity and maintainability is gaining momentum whether you have a complex business scenario to scale out or a just a CRUD system a bit more sophisticated than plain CRUD.

Transcript of Implementing Event Sourcing in .NET

ImplementingEvent Sourcing in .NET

Andrea SaltarelloCEO @ managed/designs

[email protected]://twitter.com/andysal74http://slideshare.net/andysal

Who I am

1. CEO @ Managed Designs, striving to discover the perfect«sustainable software development process»

• Customer has to be satisfied and pay

• Supplier has to have a reasonable markup

• Team members should be satisfied with their work

2. Founder and leader of UGIdotNET (first Italian .NET User Group): I need to share experiences in a peer to peer way

3. (Co)Author (together with Dino) of .NET: ArchitectingApplications for the Enterprise, by Microsoft Press

4. Basically, a software developer

CQRS recap

Query Command

DEMO

CQRS recap

It really became clear to me in the last couple of years that we need a new building block and that is the Domain Event.

[Eric Evans]

An event is something that has happened in the past.

[Greg Young]

A domain event … captures the memory of something interesting which affects the domain

[Martin Fowler]

Why events?

«Undo principle»: Instead of focusing on a system’s last known state, we could note down every occurring event: this way, we would be able build the state the system was in at any point in time

To cut a long story short: we end up recording an event stream

JobOrderCreated InvoiceIssuedJobOrderExtended JobOrderClosed

Event Stream vs. «My application»

Still, my users are more interested in knowing a job order’s balance or whether an invoice hasbeen paid.

Ways to achieve that:

• Rebuilding the state traversing the wholeevent stream (slow, but snapshots can help)

• CQRS to the rescue: let’s have a database dedicated to store the last known state and have it read by the read model

CQRS/ES in a nutshell

1. Application sends a command to the system

2. Command execution alters the system’s state and raises events to state success/failure

3. Events are notified to interested subscribers, suchas:

• Workflow managers (a.k.a. «Sagas») which can execute more commands

• Denormalizers, which will update the readmodel

Note: command/event dispatch will usually be managed by a Mediator («bus»)

Read

Model

Application

Layer

Domain

layer

Event

store

QUERY

COMMAND

Bu

s

EVENT Handler

Handler

Handler

Snapshot

DB

CQRS/ES at a glance

DEMO

Merp

On a technology side…

Looking for a bus and an event store: build or buy?

• Event Store:

–SQLWhatever

–MongoDB

–RavenDB

• Bus:

–NEventStore

–NServiceBus

My advice? Buy#donoteverthinkaboutmakingyourowntoolkit

The “Buy” Side of the Force

SQLWhatever MongoDB RavenDB

• Everyone’s skill• Every tool you can

need does exist• Both On-Premise

& cloud

• Free• Runs on both

Linux and Windows

• Both On-Premise & cloud

• Schemaless

• LINQ support• TX+DTC support• Schemaless• Both On-Premise

& cloud

MSMQ NEventStore NServiceBus

• ROTFL • Free• FOSS• DDD/CQRS/ES full

stack

• Advances configoptions

• Advancednotificationsfeatures

DEMO

NServiceBus

Bibliography

[DDD] Domain Driven Design, Eric Evans , Addison-Wesley

What I've learned about DDD since the book, Eric Evans, InfoQ

[NAAE] Microsoft .NET: Architecting Applications for the Enterprise (2° ed.), Andrea Saltarello & Dino Esposito, Microsoft Press

[MERP] Merp, https://naa4e.codeplex.com/