ktsu.ScopedAction
1.1.35
Prefix Reserved
dotnet add package ktsu.ScopedAction --version 1.1.35
NuGet\Install-Package ktsu.ScopedAction -Version 1.1.35
<PackageReference Include="ktsu.ScopedAction" Version="1.1.35" />
<PackageVersion Include="ktsu.ScopedAction" Version="1.1.35" />
<PackageReference Include="ktsu.ScopedAction" />
paket add ktsu.ScopedAction --version 1.1.35
#r "nuget: ktsu.ScopedAction, 1.1.35"
#:package ktsu.ScopedAction@1.1.35
#addin nuget:?package=ktsu.ScopedAction&version=1.1.35
#tool nuget:?package=ktsu.ScopedAction&version=1.1.35
ktsu.ScopedAction
A lightweight utility for executing paired actions at the start and end of code blocks.
Introduction
ktsu.ScopedAction is a .NET utility that provides an abstract base class for executing actions at the beginning and end of code blocks. It implements the RAII (Resource Acquisition Is Initialization) pattern and leverages C#'s using statement and the IDisposable pattern to ensure that paired operations (like resource acquisition/release, state changes, or logging) are properly executed, even in the presence of exceptions.
As an abstract class, ScopedAction is designed to be inherited and extended to create specialized scoped behavior classes tailored to specific use cases.
Features
- Abstract Base Class: Provides a foundation for creating specialized scoped action classes
- RAII Pattern: Implements Resource Acquisition Is Initialization for deterministic resource management
- Paired Actions: Execute actions when entering and exiting a scope
- Exception Safety: Cleanup actions execute even if exceptions occur
- Lightweight: Simple API with minimal overhead
- Inheritance-Based: Designed to be extended for domain-specific implementations
- Flexible: Works with any action delegates through protected constructor
- Resource Management: Follows .NET's standard disposal pattern
RAII (Resource Acquisition Is Initialization)
ktsu.ScopedAction implements the RAII pattern, a programming idiom that binds the life cycle of a resource to the lifetime of an object. This ensures that:
- Automatic Resource Management: Resources are automatically acquired when the object is constructed and released when it's destroyed
- Exception Safety: Resources are properly released even if exceptions occur within the scope
- Deterministic Execution: The OnClose action is guaranteed to execute when the object goes out of scope
- Stack-Based Semantics: Leverages C#'s
usingstatement to provide execution tied to lexical scope, mimicking C++ stack-based object destruction
The pattern is particularly useful for scenarios like:
- File operations (open/close)
- Database transactions (begin/commit or rollback)
- Lock management (acquire/release)
- Performance timing (start/stop)
- Temporary state changes (set/restore)
Installation
Package Manager Console
Install-Package ktsu.ScopedAction
.NET CLI
dotnet add package ktsu.ScopedAction
Package Reference
<PackageReference Include="ktsu.ScopedAction" Version="x.y.z" />
Usage Examples
The ScopedAction class supports three main patterns, progressing from simple to more complex scenarios:
Example 1: Using static methods without parameters
using ktsu.ScopedAction;
public class ConsoleMarkerScope() : ScopedAction(Enter, Exit)
{
// Using method groups - no lambdas needed when methods match Action signature
private static void Enter() => Console.WriteLine("Entering scope");
private static void Exit() => Console.WriteLine("Exiting scope");
}
// Usage
using (new ConsoleMarkerScope())
{
// Any code here...
Console.WriteLine("Inside the scope");
}
// Output:
// Entering scope
// Inside the scope
// Exiting scope
Example 2: Using static methods with parameters
using ktsu.ScopedAction;
public class LoggingScope(string operation)
: ScopedAction(() => Enter(operation), () => Exit(operation))
{
// Using lambdas to capture constructor parameters for static methods
private static void Enter(string operation) => Console.WriteLine($"Entering: {operation}");
private static void Exit(string operation) => Console.WriteLine($"Exiting: {operation}");
}
// Usage
using (new LoggingScope("my operation"))
{
// Any code here...
Console.WriteLine("Inside the scope");
}
// Output:
// Entering: my operation
// Inside the scope
// Exiting: my operation
Example 3: Using instance members
using ktsu.ScopedAction;
// This approach enables access to instance members in the OnClose action
public class TimingScope : ScopedAction
{
private readonly DateTime startTime; // Instance field
private readonly string operation; // Instance field
public TimingScope(string operation)
{
this.operation = operation;
this.startTime = DateTime.Now;
// OnClose can reference instance method that accesses instance members
OnClose = LogExecutionTime;
// No need to assign an OnOpen action - it would execute immediately anyway.
// Instead, just perform the "on open" logic directly in the constructor.
Console.WriteLine($"Starting: {operation}");
}
// Instance method with access to instance fields
private void LogExecutionTime()
{
// Can directly access instance members: startTime, operation
var elapsed = DateTime.Now - startTime;
Console.WriteLine($"Completed: {operation} in {elapsed.TotalMilliseconds:F2}ms");
}
}
// Usage
using (new TimingScope("database query"))
{
// Simulate some work
Thread.Sleep(100);
Console.WriteLine("Executing query...");
}
// Output:
// Starting: database query
// Executing query...
// Completed: database query in 100.xx ms
Choosing the Right Pattern
Example 1 (Method Groups): Use when you have simple static methods with no parameters. This is the most concise approach.
Example 2 (Lambda Capture): Use when you need to pass constructor parameters to static methods. Lambdas capture the parameters from the constructor scope.
Example 3 (Instance Members): Use when your OnClose logic needs access to instance state (fields, properties, methods). This pattern is essential for:
- Complex resource management
- Stateful cleanup operations
- Scenarios where disposal behavior depends on data initialized during construction
The parameterless constructor approach gives you full access to the object's state, while the action-based constructors are limited to static methods and captured parameters.
API Reference
ScopedAction Class
An abstract base class for executing actions at scope boundaries. This class must be inherited to create concrete implementations.
Constructors
| Constructor | Parameters | Description |
|---|---|---|
ScopedAction(Action? onOpen, Action? onClose) |
onOpen: Action executed on construction<br>onClose: Action executed on disposal |
Protected constructor for derived classes that executes the onOpen action immediately and stores the onClose action for later execution during disposal |
ScopedAction() |
None | Protected parameterless constructor for derived classes that need custom initialization |
Properties
| Property | Type | Description |
|---|---|---|
OnClose |
Action? |
Protected property that stores the action to execute when the scoped action is disposed. Can be set by derived classes. |
Methods
| Method | Return Type | Description |
|---|---|---|
Dispose() |
void |
Public method that implements the IDisposable interface. Executes the OnClose action if not already disposed and suppresses finalization. |
Dispose(bool disposing) |
void |
Protected virtual method for implementing the standard .NET dispose pattern. Executes the OnClose action when disposing is true and handles multiple disposal calls safely. |
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (10)
Showing the top 5 NuGet packages that depend on ktsu.ScopedAction:
| Package | Downloads |
|---|---|
|
ktsu.ImGuiStyler
A library for expressively styling ImGui.NET interfaces. |
|
|
ktsu.ImGuiApp
A comprehensive .NET library that provides complete application scaffolding for Dear ImGui applications, featuring window management, DPI-aware rendering, precision PID-controlled frame limiting with comprehensive auto-tuning, advanced font handling with Unicode/emoji support, texture management, and debug tooling. Built on Silk.NET for cross-platform OpenGL support and Hexa.NET.ImGui for modern Dear ImGui bindings. |
|
|
ktsu.ImGuiWidgets
A library of custom widgets using ImGui.NET and utilities to enhance ImGui-based applications. |
|
|
ktsu.ImGuiPopups
A library for custom popups using ImGui.NET. |
|
|
ktsu.ImGui.Popups
A professional library for modal dialogs and popup components in ImGui.NET, providing message boxes, input prompts with validation (string, int, float), searchable selection lists with type-safe generics, and an advanced filesystem browser with open/save modes, directory navigation, and pattern filtering support. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.35 | 0 | 8/26/2026 |
| 1.1.34 | 56 | 8/24/2026 |
| 1.1.33 | 272 | 8/21/2026 |
| 1.1.32 | 362 | 8/20/2026 |
| 1.1.31 | 536 | 8/19/2026 |
| 1.1.30 | 581 | 8/18/2026 |
| 1.1.29 | 276 | 8/17/2026 |
| 1.1.28 | 1,376 | 8/6/2026 |
| 1.1.27 | 388 | 8/5/2026 |
| 1.1.26 | 752 | 7/29/2026 |
| 1.1.25 | 1,277 | 7/22/2026 |
| 1.1.24 | 648 | 7/16/2026 |
| 1.1.23 | 279 | 7/15/2026 |
| 1.1.22 | 244 | 7/14/2026 |
| 1.1.21 | 1,126 | 7/9/2026 |
| 1.1.20 | 916 | 7/2/2026 |
| 1.1.19 | 354 | 7/1/2026 |
| 1.1.18 | 316 | 6/30/2026 |
| 1.1.17 | 646 | 6/28/2026 |
| 1.1.16 | 119 | 6/28/2026 |
## v1.1.35 (patch)
Changes since v1.1.34:
- ci: make the SonarQube quality gate opt in [patch] ([@matt-edmondson](https://github.com/matt-edmondson))
- ci: adopt the unified dotnet workflow [patch] ([@matt-edmondson](https://github.com/matt-edmondson))