BlazingSingularity 1.0.0-beta.1

This is a prerelease version of BlazingSingularity.
There is a newer prerelease version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazingSingularity" Version="1.0.0-beta.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BlazingSingularity" Version="1.0.0-beta.1" />
                    
Directory.Packages.props
<PackageReference Include="BlazingSingularity" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BlazingSingularity --version 1.0.0-beta.1
                    
#r "nuget: BlazingSingularity, 1.0.0-beta.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BlazingSingularity@1.0.0-beta.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BlazingSingularity&version=1.0.0-beta.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=BlazingSingularity&version=1.0.0-beta.1&prerelease
                    
Install as a Cake Tool

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 AsyncRelayCommand generation for Task returning methods
  • IsLoading property - Track async command execution state
  • CanExecute support - 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

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 command
  • void Execute(object? parameter) - Synchronous execution (fire-and-forget)
  • bool CanExecute(object? parameter) - Check if command can execute
  • bool IsLoading - Track if async operation is in progress
  • bool IsCancellable - Check if command supports cancellation
  • void CancelCommand() - Cancel the currently executing operation
  • event EventHandler? CanExecuteChanged - Notification when CanExecute changes
  • event EventHandler? IsLoadingChanged - Notification when IsLoading changes

RelayCommand<T> / RelayCommand

  • void Execute(object? parameter) - Execute the command
  • bool CanExecute(object? parameter) - Check if command can execute
  • event EventHandler? CanExecuteChanged - Notification when CanExecute changes

CanExecute Support

The source generator automatically detects and wires up CanExecute methods:

  1. Naming Convention: For a method MyMethod, create a method named CanMyMethod that returns bool
  2. Parameter Matching: The CanExecute method must have the same parameters as the command method
  3. Automatic Wiring: The generator automatically passes the CanExecute method to the command constructor
  4. 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

  1. Class must be partial - The class using [RelayCommand] must be declared as partial
  2. Supported signatures:
    • void MethodName() → RelayCommand
    • void MethodName(T param) → RelayCommand<T>
    • Task MethodName() → AsyncRelayCommand
    • Task MethodName(T param) → AsyncRelayCommand<T>
    • Task MethodName(CancellationToken ct) → AsyncRelayCommand (Cancellable)
    • Task MethodName(T param, CancellationToken ct) → AsyncRelayCommand<T> (Cancellable)
  3. Naming convention - Method MyMethod generates property MyMethodCommand
  4. IsLoading prevents execution - When IsLoading is true, CanExecute automatically returns false
  5. CanExecute methods - Must match parameter signature and return bool (excluding CancellationToken)
  6. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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