BlazingSingularity 1.0.0-beta.1
See the version list below for details.
dotnet add package BlazingSingularity --version 1.0.0-beta.1
NuGet\Install-Package BlazingSingularity -Version 1.0.0-beta.1
<PackageReference Include="BlazingSingularity" Version="1.0.0-beta.1" />
<PackageVersion Include="BlazingSingularity" Version="1.0.0-beta.1" />
<PackageReference Include="BlazingSingularity" />
paket add BlazingSingularity --version 1.0.0-beta.1
#r "nuget: BlazingSingularity, 1.0.0-beta.1"
#:package BlazingSingularity@1.0.0-beta.1
#addin nuget:?package=BlazingSingularity&version=1.0.0-beta.1&prerelease
#tool nuget:?package=BlazingSingularity&version=1.0.0-beta.1&prerelease
BlazingSingularity - ICommand for Blazor
A WPF-style commanding pattern for Blazor using source generators.
Features
[RelayCommand]attribute - Decorate methods to automatically generate command properties- Async support - Automatic
AsyncRelayCommandgeneration forTaskreturning methods IsLoadingproperty - Track async command execution stateCanExecutesupport - Control command availability- Parameter support - Commands support 0 or 1 parameter
- CancellationToken support - Cancel long-running async operations mid-execution
- Fluent Builder API - Create commands programmatically without attributes
- Source generator - Zero-overhead code generation at compile time
Installation
NuGet Package (Recommended)
dotnet add package BlazingSingularity --prerelease
Or via the Package Manager Console:
Install-Package BlazingSingularity -Pre
The NuGet package includes both the library and the source generator automatically.
Project Reference (Development)
If you're developing locally, add reference to both projects in your .csproj:
<ItemGroup>
<ProjectReference Include="..\BlazingSingularity\BlazingSingularity.csproj" />
<ProjectReference Include="..\BlazingSingularity.SourceGenerators\BlazingSingularity.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
Usage
1. Create a Partial Component Code-Behind
using BlazingSingularity.Commands;
using Microsoft.AspNetCore.Components;
namespace YourNamespace;
public partial class MyComponent : ComponentBase
{
private WeatherForecast[]? forecasts;
// Async command with automatic CanExecute support
[RelayCommand]
private async Task LoadForecast()
{
await Task.Delay(1500);
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
LoadForecastCommand.RaiseCanExecuteChanged();
ClearCommand.RaiseCanExecuteChanged();
StateHasChanged();
}
// CanExecute method - automatically discovered by source generator
private bool CanLoadForecast()
{
// Can only load if we don't already have forecasts
return forecasts == null;
}
[RelayCommand]
private void Clear()
{
forecasts = null;
LoadForecastCommand.RaiseCanExecuteChanged();
ClearCommand.RaiseCanExecuteChanged();
}
private bool CanClear()
{
// Can only clear if we have forecasts
return forecasts != null;
}
}
2. Use Generated Commands in Razor
@inherits ComponentBase
<div>
<button @onclick="() => LoadDataCommand.ExecuteAsync(\"test\")"
disabled="@(!LoadDataCommand.CanExecute(\"test\"))">
@if (LoadDataCommand.IsLoading)
{
<span>Loading...</span>
}
else
{
<span>Load Data</span>
}
</button>
<button @onclick="() => SaveDataCommand.ExecuteAsync(null)"
disabled="@(!SaveDataCommand.CanExecute(null))">
@if (SaveDataCommand.IsLoading)
{
<span>Saving...</span>
}
else
{
<span>Save</span>
}
</button>
</div>
Generated Code
For a method named LoadForecast with a CanLoadForecast method, the source generator creates:
private AsyncRelayCommand? _loadForecastCommand;
public AsyncRelayCommand LoadForecastCommand => _loadForecastCommand ??= new(LoadForecast, CanLoadForecast);
Without a CanExecute method:
private AsyncRelayCommand? _loadForecastCommand;
public AsyncRelayCommand LoadForecastCommand => _loadForecastCommand ??= new(LoadForecast);
Command Properties
AsyncRelayCommand<T> / AsyncRelayCommand
Task ExecuteAsync(object? parameter)- Execute the async commandvoid Execute(object? parameter)- Synchronous execution (fire-and-forget)bool CanExecute(object? parameter)- Check if command can executebool IsLoading- Track if async operation is in progressbool IsCancellable- Check if command supports cancellationvoid CancelCommand()- Cancel the currently executing operationevent EventHandler? CanExecuteChanged- Notification when CanExecute changesevent EventHandler? IsLoadingChanged- Notification when IsLoading changes
RelayCommand<T> / RelayCommand
void Execute(object? parameter)- Execute the commandbool CanExecute(object? parameter)- Check if command can executeevent EventHandler? CanExecuteChanged- Notification when CanExecute changes
CanExecute Support
The source generator automatically detects and wires up CanExecute methods:
- Naming Convention: For a method
MyMethod, create a method namedCanMyMethodthat returnsbool - Parameter Matching: The
CanExecutemethod must have the same parameters as the command method - Automatic Wiring: The generator automatically passes the
CanExecutemethod to the command constructor - Manual Notification: Call
MyMethodCommand.RaiseCanExecuteChanged()when conditions change
Example:
[RelayCommand]
private async Task LoadData(string filter)
{
// ... load data
LoadDataCommand.RaiseCanExecuteChanged(); // Notify that CanExecute state may have changed
}
private bool CanLoadData(string filter)
{
return !string.IsNullOrEmpty(filter); // Only allow execution if filter is not empty
}
CancellationToken Support
Commands can be cancelled mid-execution for long-running operations:
Using Source Generator
Simply add a CancellationToken parameter to your method:
[RelayCommand]
private async Task LoadData(CancellationToken cancellationToken)
{
await Task.Delay(5000, cancellationToken);
// Long-running operation that respects cancellation
}
The source generator automatically detects the CancellationToken and creates a cancellable command.
In Razor
<button @onclick="() => LoadDataCommand.ExecuteAsync()"
disabled="@LoadDataCommand.IsLoading">
Load Data
</button>
@if (LoadDataCommand.IsLoading && LoadDataCommand.IsCancellable)
{
<button @onclick="() => LoadDataCommand.CancelCommand()"
class="btn-danger">
Cancel
</button>
}
With Parameters
[RelayCommand]
private async Task SearchData(string query, CancellationToken cancellationToken)
{
await Task.Delay(3000, cancellationToken);
// Search operation
}
The CancellationToken is always the last parameter and is automatically handled by the command infrastructure.
Fluent Builder API
Create commands programmatically without using attributes:
Sync Commands
public ICommand SaveCommand => Commands.Create()
.WithExecute(() =>
{
// Save logic
})
.WithCanExecute(() => _hasChanges)
.Build();
Async Commands
public IAsyncCommand LoadCommand => Commands.CreateAsync()
.WithExecute(async () =>
{
await LoadDataAsync();
})
.WithCanExecute(() => !string.IsNullOrEmpty(_filter))
.Build();
Async Commands with Cancellation
public IAsyncCommand SearchCommand => Commands.CreateAsync<string>()
.WithCancellation(async (query, ct) =>
{
await Task.Delay(3000, ct);
// Search with cancellation support
})
.WithCanExecute(query => !string.IsNullOrWhiteSpace(query))
.Build();
Parameterized Commands
// Sync with parameter
public ICommand DeleteCommand => Commands.Create<int>()
.WithExecute(id => DeleteItem(id))
.WithCanExecute(id => id > 0)
.Build();
// Async with parameter
public IAsyncCommand UpdateCommand => Commands.CreateAsync<Item>()
.WithExecute(async item => await UpdateItemAsync(item))
.Build();
Important Notes
- Class must be
partial- The class using[RelayCommand]must be declared aspartial - Supported signatures:
void MethodName()→RelayCommandvoid MethodName(T param)→RelayCommand<T>Task MethodName()→AsyncRelayCommandTask MethodName(T param)→AsyncRelayCommand<T>Task MethodName(CancellationToken ct)→AsyncRelayCommand(Cancellable)Task MethodName(T param, CancellationToken ct)→AsyncRelayCommand<T>(Cancellable)
- Naming convention - Method
MyMethodgenerates propertyMyMethodCommand - IsLoading prevents execution - When
IsLoadingistrue,CanExecuteautomatically returnsfalse - CanExecute methods - Must match parameter signature and return
bool(excluding CancellationToken) - CancellationToken - Always the last parameter, automatically handled by command infrastructure
Advanced Scenarios
Multiple Filter Parameters
For complex filtering with many parameters, use Bound Commands that capture property values at execution time:
public partial class MyComponent : ComponentBase
{
// Filter properties bound to UI controls
private string searchText = string.Empty;
private int? minValue;
private int? maxValue;
private bool filterActive = true;
// Bound command captures all properties when executed
private IAsyncCommand? _searchCommand;
public IAsyncCommand SearchCommand => _searchCommand ??= Commands.CreateAsync()
.WithCancellation(async (ct) =>
{
// Properties captured at execution time
var results = await SearchAsync(searchText, minValue, maxValue, filterActive, ct);
await InvokeAsync(StateHasChanged);
})
.WithCanExecute(() => !string.IsNullOrWhiteSpace(searchText))
.AsBound() // Makes it capture properties at execution time
.Build();
private void OnFilterChanged()
{
SearchCommand.RaiseCanExecuteChanged();
}
}
See ADVANCED_FILTERING_EXAMPLE.md for complete examples.
Project Structure
BlazingSingularity/
├── Commands/
│ ├── ICommand.cs - Command interface
│ ├── IAsyncCommand.cs - Async command interface
│ ├── RelayCommand.cs - Synchronous command implementation
│ ├── AsyncRelayCommand.cs - Async command implementation
│ └── RelayCommandAttribute.cs - Attribute for source generation
└── ExampleComponent.razor.cs - Example usage
BlazingSingularity.SourceGenerators/
└── RelayCommandGenerator.cs - Source generator implementation
Example
See ExampleComponent.razor and ExampleComponent.razor.cs for a complete working example.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Microsoft.AspNetCore.Components.Web (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-beta.6 | 131 | 3/19/2026 |
| 1.0.0-beta.5 | 208 | 3/18/2026 |
| 1.0.0-beta.4 | 117 | 3/11/2026 |
| 1.0.0-beta.3 | 172 | 2/7/2026 |
| 1.0.0-beta.2 | 110 | 2/5/2026 |
| 1.0.0-beta.1 | 958 | 12/4/2025 |