ApplicationEventBusAware class abstract interface

🫘 Interface for components that need access to the ApplicationEventBus.

The ApplicationEventBusAware interface provides components with direct access to the application's event bus for publishing and subscribing to application events in an event-driven architecture.

Event-Driven Architecture Benefits:

  • Loose Coupling: Components communicate through events without direct dependencies
  • Asynchronous Processing: Support for async event handling
  • Extensibility: Easy to add new event listeners without modifying publishers
  • Audit Trail: Events provide natural audit points in system behavior

Framework Integration:

  • Called during component initialization when event bus is available
  • The event bus may be null if event system is disabled
  • Events are typically processed synchronously in the calling thread

Example Usage:

@Component
class OrderService implements ApplicationEventBusAware {
  ApplicationEventBus? _eventBus;

  @override
  void setApplicationEventBus(ApplicationEventBus? applicationEventBus) {
    _eventBus = applicationEventBus;
  }

  Future<Order> createOrder(OrderRequest request) async {
    final order = Order.fromRequest(request);
    
    // Publish domain event
    _eventBus?.publish(OrderCreatedEvent(this, order));
    
    // Publish integration event
    _eventBus?.publish(OrderPlacedEvent(this, order));
    
    return order;
  }

  void subscribeToPaymentEvents() {
    _eventBus?.subscribe<PaymentReceivedEvent>((event) {
      _fulfillOrder(event.orderId);
    });
  }
}

Best Practices:

  • Use domain events for business logic, system events for technical concerns
  • Keep event objects immutable and serializable
  • Consider event ordering and idempotency in distributed systems
  • Use @EventListener annotation for simpler event handling when possible

See also:

Properties

hashCode β†’ int
The hash code for this object.
no setterinherited
runtimeType β†’ Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) β†’ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setApplicationEventBus(ApplicationEventBus? applicationEventBus) β†’ void
Sets the ApplicationEventBus that this component can use for event operations.
toString() β†’ String
A string representation of this object.
inherited

Operators

operator ==(Object other) β†’ bool
The equality operator.
inherited