Use Polymorphic Handler Inheritance
mainHandlers resolve polymorphically.
- Notifications: A handler registered for a base type (e.g.,
Notification) will receive all derived notification types. - Requests: A more specific handler takes priority over a base-type handler if both are registered.
sealed class DomainEvent : Notification
data class OrderPlaced(val orderId: Long) : DomainEvent()
data class OrderShipped(val orderId: Long) : DomainEvent()
// Receives every DomainEvent subtype
class AuditLogger : NotificationHandler<DomainEvent> {
override suspend fun handle(notification: DomainEvent) {
audit.record(notification)
}
}