Skip to content

Tutorial: Build an E-Commerce App

In this tutorial, you'll build an event-sourced e-commerce application from scratch. By the end, you'll have a working system with products, orders, customers, and reactive read models.

What You'll Build

  • Product aggregate — create products, manage prices and stock
  • Order aggregate — place orders with line items, cancel orders
  • Customer aggregate — manage customer profiles and addresses
  • Product Catalog projection — a read model that stays in sync with product events

What You'll Learn

ChapterConcept
1. Project SetupGo module, in-memory backends, bootstrap
2. Your First Aggregateaggregate.Base, events, appliers
3. Events & StateMultiple events, typed event data
4. Codec RegistryEvent serialization
5. RepositoriesSaving and fetching aggregates
6. CommandsCommand bus, dispatching, handlers
7. The OrderSecond aggregate, validation patterns
8. The CustomerValue objects, addresses
9. Aggregate SplittingSplitting aggregates, shared UUIDs
10. ProjectionsRead models, schedules
11. Production BackendsMongoDB, NATS, PostgreSQL
12. TestingTesting aggregates and projections

Prerequisites

  • Basic Go knowledge (structs, interfaces, generics)
  • Familiarity with event sourcing and DDD concepts (see Introduction)

The Colocation Pattern

Throughout this tutorial, we keep everything related to an aggregate in a single file. Event names, event data types, commands, the aggregate struct, business methods, and event appliers all live together. This makes aggregates self-contained and easy to understand.

shop/
  main.go           # Bootstrap, wiring
  product.go        # Product aggregate + events + commands
  pricing.go        # Pricing aggregate (split from Product)
  order.go          # Order aggregate + events + commands
  customer.go       # Customer aggregate + events + commands
  catalog.go        # Product catalog projection

Let's get started.