Interface ICommandExecutionScope

Namespace
Cratis.Arc.Commands
Assembly
Cratis.Arc.Core.dll

Defines a scope that participates in the execution of a command, beginning before the command's filters and handler run and completing with the final CommandResult — whether the command succeeded or not.

public interface ICommandExecutionScope
Extension Methods

Remarks

Implementations are discovered automatically and invoked by the command pipeline around every command execution. This is the extension point for bracketing a command with cross-cutting lifetime concerns, such as making the command a transactional scope. Begin(CommandContext) is deliberately synchronous so ambient state an implementation establishes — for example an AsyncLocal-based unit of work — flows to the command's execution. A scope can also implement IAsyncCommandExecutionScope for awaited initialization after all synchronous begins and before filters. Validation-only calls do not run either lifecycle. Scopes nest: they complete in the reverse of the order they began. The relative order between different scope implementations is unspecified — implementations must be independent of each other. Every scope completes in isolation: a failure completing one scope never prevents the remaining scopes from completing. Complete(CommandContext, CommandResult) can be invoked when Begin(CommandContext) never ran for the implementation (for example when another scope's Begin(CommandContext) threw), and implementations must tolerate that.

Methods

Begin(CommandContext)

Begins the scope for the command about to execute.

void Begin(CommandContext context)

Parameters

context CommandContext

The CommandContext for the command.

Complete(CommandContext, CommandResult)

Completes the scope with the final result of the command, called exactly once for every outcome — success, validation failure or exception. Implementations can mutate the CommandResult to reflect the outcome of completing the scope. An exception thrown from this method is folded into the command's result as an exception outcome rather than propagating to the caller.

Task Complete(CommandContext context, CommandResult result)

Parameters

context CommandContext

The CommandContext for the command.

result CommandResult

The final, mutable CommandResult for the command.

Returns

Task

Awaitable task.