eventHandler method

  1. @override
void eventHandler(
  1. Event event
)

Apply an event to update the aggregate state This is called during both command processing and recovery

Implementation

@override
void eventHandler(Event event) {
  // Ensure state is initialized before processing events
  // This is critical during recovery when the first event is replayed
  ensureStateInitialized();

  if (event is! InvoiceEvent) {
    throw ArgumentError('Expected InvoiceEvent, got ${event.runtimeType}');
  }

  switch (event.runtimeType) {
    case InvoiceCreatedEvent:
      _applyInvoiceCreated(event as InvoiceCreatedEvent);
      break;
    case InvoiceStatusChangedEvent:
      _applyInvoiceStatusChanged(event as InvoiceStatusChangedEvent);
      break;
    case InvoicePaidEvent:
      _applyInvoicePaid(event as InvoicePaidEvent);
      break;
    case InvoiceExpiredEvent:
      _applyInvoiceExpired(event as InvoiceExpiredEvent);
      break;
    case InvoiceCancelledEvent:
      _applyInvoiceCancelled(event as InvoiceCancelledEvent);
      break;
    default:
      throw ArgumentError('Unknown event type: ${event.runtimeType}');
  }
}