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
| Chapter | Concept |
|---|---|
| 1. Project Setup | Go module, in-memory backends, bootstrap |
| 2. Your First Aggregate | aggregate.Base, events, appliers |
| 3. Events & State | Multiple events, typed event data |
| 4. Codec Registry | Event serialization |
| 5. Repositories | Saving and fetching aggregates |
| 6. Commands | Command bus, dispatching, handlers |
| 7. The Order | Second aggregate, validation patterns |
| 8. The Customer | Value objects, addresses |
| 9. Aggregate Splitting | Splitting aggregates, shared UUIDs |
| 10. Projections | Read models, schedules |
| 11. Production Backends | MongoDB, NATS, PostgreSQL |
| 12. Testing | Testing 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 projectionLet's get started.