Interface IActionHandler<TAction, TResult>
Defines a handler for an action that returns a result. This is the recommended handler interface for queries.
public interface IActionHandler<in TAction, TResult> where TAction : IDispatchAction<TResult>
Type Parameters
TActionThe type of action to handle.
TResultThe type of result returned by the handler.
- Extension Methods
Remarks
Recommended for most use cases. Return your business type directly; the framework automatically wraps it in IMessageResult<T>. Use exception throwing for failures; the exception mapping middleware converts exceptions to proper failure results.
Common use cases include:
- Query handlers that fetch and return data
- Commands that return created entity IDs
- Validation operations that return detailed results
- Calculation or transformation operations
For advanced scenarios requiring direct control over IMessageResult<T> (e.g., a result served from cache, custom result metadata), use IDispatchHandler<TMessage> instead.
Methods
HandleAsync(TAction, CancellationToken)
Handles the specified action and returns a result asynchronously.
Task<TResult> HandleAsync(TAction action, CancellationToken cancellationToken)
Parameters
actionTActionThe action to handle.
cancellationTokenCancellationTokenThe cancellation token to observe.
Returns
- Task<TResult>
A task containing the result of the action.
Remarks
Implementations should respect the cancellation token and handle exceptions appropriately. The returned result will be wrapped in an IMessageResult by the framework for transport across boundaries.