CompensationAction

Marker interface for user-defined typed compensation actions.

This interface allows users to define strongly-typed compensation actions using sealed classes or enums, enabling type-safe compensation logic.

Usage Example

sealed interface OrderCompensationAction : CompensationAction {
data class ReleaseInventory(val reservationId: String) : OrderCompensationAction
data class RefundPayment(val transactionId: String) : OrderCompensationAction
data class CancelShipping(val shipmentId: String) : OrderCompensationAction
}

// Use in saga steps
step("charge-payment") { context ->
val txId = paymentService.charge(context.amount)
PaymentResult(txId)
}
.compensate { result ->
// Create typed compensation action
val action = OrderCompensationAction.RefundPayment(result.transactionId)
compensationService.execute(action)
}

Benefits

  • Type safety for compensation actions

  • Pattern matching with sealed classes

  • Clear documentation of all possible compensations

  • Easy serialization for compensation queues (if needed)

Since

1.0.0