Lab 06: Event Sourcing
Overview
Step 1: Core Event Types
package eventsourcing
import (
"encoding/json"
"time"
)
// Event: immutable fact that something happened
type Event struct {
AggregateID string
Type string
Version int
Data json.RawMessage
OccurredAt time.Time
Metadata map[string]string // correlation_id, causation_id, user_id
}
// Aggregate: entity rebuilt from its event stream
type Aggregate interface {
AggregateID() string
Version() int
Apply(event Event)
UncommittedEvents() []Event
MarkEventsAsCommitted()
}
// EventStore: append-only log of domain events
type EventStore interface {
Append(aggregateID string, expectedVersion int, events []Event) error
Load(aggregateID string) ([]Event, error)
LoadFrom(aggregateID string, fromVersion int) ([]Event, error)
}Step 2: In-Memory EventStore with Optimistic Concurrency
Step 3: Order Aggregate
Step 4: Projection — Read Model
Step 5: Snapshot Pattern
Step 6: Repository Pattern for Aggregates
Step 7: Event Bus for Cross-Aggregate Communication
Step 8: Capstone — Event Sourcing Demo
Summary
Component
Pattern
Guarantee
Last updated
