Scoped Runner

H.Necessaire's ScopedRunner is a sugar syntax for leveraging C# using keyword

Scoped Runner
Photo by Mathieu Turle / Unsplash
dotnet add package H.Necessaire

H.Necessaire

The scoped runner is a simple IDisposable implementation that's useful for leveraging the usage keyword sugar syntax for try { } finally { }.

Example use case

using (new ScopedRunner(onStart: () => isMigrating = true, onStop: () => isMigrating = false))
{
  //Run data migrations
}

Sugar syntax for try-finally via usage and ScopedRunner

Specific derivatives

There a few specific derived classes for easier usage syntax

TimeMeasurement

using (new TimeMeasurement(x => logger.LogTrace($"DONE Running {processorName} for {action} in {x}")))
{
    OperationResult processingResult = await ProcessQdAction(action);
    result = processingResult.WithPayload(action).ToQdActionResult();
}

ConsumerScope

class ConsumerScope : ScopedRunner
{
  ConsumerScope(ConsumerIdentity consumer)
  ConsumerScope(OperationContext operationContext)
  ConsumerScope(SyncRequest syncRequest)
}

OperationContextScope

class OperationContextScope : ScopedRunner
{
  OperationContextScope(OperationContext operationContext)
  OperationContextScope(SyncRequest syncRequest)
}

ProgressiveScope

class ProgressiveScope : ScopedRunner
{
  ProgressiveScope(string scopeIdentifier, AsyncEventHandler<ProgressEventArgs> onProgress = null)
}