OperationContext<TResult>

Namespace: SnowBank.Threading.Operations · class

Implements: IOperationContext<TResult>, IOperationContext

Context of an operation

Constructors

OperationContext<TResult>

OperationContext<TResult>(OperationScheduler scheduler, string id, OperationState state, string type, string key, IOperationContext parent, CancellationTokenSource cts)

Properties

Activity

Activity Activity { get; }

Cancellation

CancellationToken Cancellation { get; }

Cancellation token used to abort this operation

Error

OperationError Error { get; }

Error produced by operation attached to this context.

HasResult

bool HasResult { get; }

Returns true if the operation attached to this context has completed successfully

This flag is required because TResult can be nullable, and a nullResult does not necessary mean that the operation failed.

Id

string Id { get; }

Globally unique identifier for this operation

Key

string Key { get; }

If not null, used to distinguish multiple concurrent operations of the type Type

Parent

IOperationContext Parent { get; }

If not null, parent operation that started this operation

Result

TResult Result { get; }

Result of the successful completion of the operation attached to this context

Scheduler

OperationScheduler Scheduler { get; }

Scheduler that handles the execution of this context

State

OperationState State { get; }

Current state of the operation

Type

string Type { get; }

Operation type (for diagnostic purpose)

Methods

Dispatch

void Dispatch(IEvent evt)

Dispatch an event that happened during the processing of this operation

ExecuteStep

IDisposable ExecuteStep(string id, string label = null)

Execute a sub-step, using the same context as this operation

  • id — Id of the sub-step (for computers)
  • label — Label of the sub-step (for humans)

Returns: Token that must be disposed at the end of the sub-step

Sub-steps should be used to wrap the various parts of an operation, that are not large enough to be split into sub-operations.

They will be exposed to observers who are following the execution.

ExecuteSubOperation

Task<OperationResult<TSubResult>> ExecuteSubOperation<TSubResult>(IOperationContext<TSubResult> operation, Func<IOperationContext<TSubResult>, Task<OperationResult<TSubResult>>> handler)

Execute a sub-operation, in its own context.

  • operation — Context of the sub-operation, created as a child of this operation
  • handler — Function that will be run to execute the sub-operation

Returns: Result of the sub-operation.

Failed

OperationResult<TResult> Failed(OperationError error)

Marks this context as failed

  • error — Description of the error

Returns: Failed operation result

Log

void Log(LogLevel level, Exception exception, string message, object[] args = null)

Success

OperationResult<TResult> Success(TResult result)

Marks this context as successful

  • result — Result of the operation

Returns: Successful operation result

TryGetResult

bool TryGetResult(out TResult result)

Returns the result attached to this context, if it ran successfully

  • result — Receives the result of the operation, if it was a success

Returns: true if the context was marked as a success; otherwise, false