1Minh đặt ba món. API tạo command PlaceOrder(customerId, lines) rồi gọi OrderCommandService.handle(PlaceOrder). Command chỉ chứa dữ liệu đầu vào, đặt tên theo ý định nghiệp vụ.Minh orders three items. The API builds the PlaceOrder(customerId, lines) command and calls OrderCommandService.handle(PlaceOrder). The command only carries input data and is named after a business intent.
2Handler gọi Order.place(...) để kiểm tra invariant (Go: len(cmd.Lines) == 0 thì báo lỗi), rồi orders.save(order) ghi orders và order_lines trong một transaction.The handler calls Order.place(...) to enforce invariants (Go: len(cmd.Lines) == 0 returns an error), then orders.save(order) writes orders and order_lines in one transaction.
3Handler gọi events.publishEvent(new OrderPlaced(id, customerId, total, 3)) và chỉ trả về order.id(), không trả view. Event mang đủ dữ liệu để phía đọc khỏi phải hỏi lại Write DB.The handler calls events.publishEvent(new OrderPlaced(id, customerId, total, 3)) and returns only order.id(), never a view. The event carries enough data so the read side never queries the Write DB.
4Sau commit, OrderSummaryProjector.on(OrderPlaced) chạy với REQUIRES_NEW và INSERT một dòng order_summary trạng thái PLACED, ON CONFLICT DO NOTHING nếu event đến lặp. Go: Projector.Run nhận event từ channel, apply dùng type switch.After commit, OrderSummaryProjector.on(OrderPlaced) runs with REQUIRES_NEW and INSERTs an order_summary row with status PLACED, ON CONFLICT DO NOTHING on redelivery. Go: Projector.Run receives the event from a channel and apply routes it with a type switch.
5Minh mở “Đơn hàng của tôi”: API gọi OrderQueryService.myOrders(customerId, limit). Query SELECT một bảng trên replica rồi map từng dòng thành OrderSummaryView, không đụng aggregate Order.Minh opens “My orders”: the API calls OrderQueryService.myOrders(customerId, limit). The query SELECTs one table on the replica and maps each row to an OrderSummaryView without touching the Order aggregate.
6Nếu replica hay projector bất đồng bộ chưa bắt kịp, danh sách tạm chưa có ORD-1001. Giao diện chỉ hỏi lại query có thời hạn, không gửi lại PlaceOrder vì sẽ tạo đơn thứ hai.If the replica or an asynchronous projector has not caught up, the list may not show ORD-1001 yet. The UI only re-queries with a deadline and never resends PlaceOrder, which would create a second order.
7Minh đổi ý: API tạo CancelOrder(orderId, reason) và gọi handle(CancelOrder). Handler findById rồi order.cancel(reason); aggregate ném lỗi nếu đơn đã giao.Minh changes his mind: the API builds CancelOrder(orderId, reason) and calls handle(CancelOrder). The handler calls findById, then order.cancel(reason); the aggregate throws if the order has shipped.
8Handler phát OrderCancelled; projector on(OrderCancelled) chạy UPDATE order_summary SET status = 'CANCELLED'. Lần đọc tiếp theo, myOrders trả OrderSummaryView mới.The handler publishes OrderCancelled; the projector's on(OrderCancelled) runs UPDATE order_summary SET status = 'CANCELLED'. On the next read, myOrders returns the updated OrderSummaryView.
9Bản Go cho thấy rủi ro: CommandHandler commit xong mới gọi Bus.Publish. Nếu publish lỗi, đơn vẫn lưu nhưng read model lệch; code chỉ ghi log, vì thế production cần Transactional Outbox.The Go version shows the risk: CommandHandler calls Bus.Publish only after commit. If publishing fails, the order is saved but the read model drifts; the code only logs it, which is why production needs a Transactional Outbox.