ICommandor 1.1.0

dotnet add package ICommandor --version 1.1.0
                    
NuGet\Install-Package ICommandor -Version 1.1.0
                    
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="ICommandor" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ICommandor" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ICommandor" />
                    
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 ICommandor --version 1.1.0
                    
#r "nuget: ICommandor, 1.1.0"
                    
#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 ICommandor@1.1.0
                    
#: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=ICommandor&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ICommandor&version=1.1.0
                    
Install as a Cake Tool

Commandor

A lightweight CQRS/Mediator library for .NET with automatic caching and source generation.

Installation

dotnet add package ICommandor

Quick Start

1. Define Your Service Interface

using Commandor;

public interface IColorService : ICommandorService
{
    [QueryHandler]
    Task<List<ColorEntity>> GetAll(GetAllColorsQuery query, CancellationToken ct = default);

    [QueryHandler]
    Task<ColorEntity?> GetById(GetColorQuery query, CancellationToken ct = default);

    [CommandHandler]
    Task<ColorEntity> Create(CreateColorCommand command, CancellationToken ct = default);

    [CommandHandler]
    Task Update(UpdateColorCommand command, CancellationToken ct = default);
}

2. Implement Your Service

public class ColorService : IColorService
{
    private readonly AppDbContext _db;
    private readonly ICommandor _commandor;

    public ColorService(AppDbContext db, ICommandor commandor)
    {
        _db = db;
        _commandor = commandor;
    }

    public async Task<ColorEntity> Create(CreateColorCommand command, CancellationToken ct = default)
    {
        var color = new ColorEntity { Name = command.Name };
        _db.Colors.Add(color);
        await _db.SaveChangesAsync(ct);
        
        // Invalidate cache after mutation
        _commandor.Invalidate<IColorService>();
        
        return color;
    }

    public Task<List<ColorEntity>> GetAll(GetAllColorsQuery query, CancellationToken ct = default)
        => _db.Colors.ToListAsync(ct);

    public Task<ColorEntity?> GetById(GetColorQuery query, CancellationToken ct = default)
        => _db.Colors.FindAsync(new object[] { query.Id }, ct).AsTask();

    public async Task Update(UpdateColorCommand command, CancellationToken ct = default)
    {
        var color = await _db.Colors.FindAsync(new object[] { command.Id }, ct);
        if (color != null)
        {
            color.Name = command.Name;
            await _db.SaveChangesAsync(ct);
            
            // Invalidate cache after mutation
            _commandor.Invalidate<IColorService>();
        }
    }
}

3. Register Services

var services = new ServiceCollection();
services.AddCommandor();
services.AddCommandorService<IColorService, ColorService>();

4. Use in Controllers

public class ColorsController : ControllerBase
{
    private readonly ICommandor _commandor;

    public ColorsController(ICommandor commandor) => _commandor = commandor;

    [HttpGet]
    public Task<List<ColorEntity>> GetAll()
        => _commandor.SendAsync(new GetAllColorsQuery());

    [HttpPost]
    public Task<ColorEntity> Create(CreateColorCommand command)
        => _commandor.SendAsync(command);
}

Features

Automatic Caching

  • [QueryHandler] results are automatically cached using IMemoryCache
  • Cache is invalidated per service using ICommandor.Invalidate<TService>()
  • No manual cache key management needed

Cache Invalidation

New in v1.0.7: Use ICommandor for cache invalidation:

// Invalidate all cached queries for a service
_commandor.Invalidate<ITodoService>();

// Or async version
await _commandor.InvalidateAsync<ITodoService>();

Source Generation

Commandor automatically generates handler classes for methods marked with [CommandHandler] or [QueryHandler]. No boilerplate code needed!

Important Notes

Command/Query Pattern

All method parameters must implement IRequest<TResponse> or IRequest. Primitive types cannot be used directly.

❌ Incorrect:

public interface ITodoService : ICommandorService
{
    [CommandHandler]
    Task<Todo> CreateTodoAsync(string title);  // ❌ string is not IRequest
    
    [QueryHandler]
    Task<Todo?> GetTodoByIdAsync(int id);  // ❌ int is not IRequest
}

✅ Correct:

// Define request/query records
public record CreateTodoCommand(string Title) : IRequest<Todo>;
public record GetTodoByIdQuery(int Id) : IRequest<Todo?>;

public interface ITodoService : ICommandorService
{
    [CommandHandler]
    Task<Todo> CreateTodoAsync(CreateTodoCommand command, CancellationToken ct = default);
    
    [QueryHandler]
    Task<Todo?> GetTodoByIdAsync(GetTodoByIdQuery query, CancellationToken ct = default);
}

Method Naming Conventions

Recommended naming patterns:

  • Commands: Create..., Update..., Delete..., Process...
  • Queries: Get..., Find..., List..., Search...

Cache TTL (Optional)

Specify cache duration per query:

[QueryHandler(CacheTtlSeconds = 300)]  // Cache for 5 minutes
Task<List<Product>> GetProducts(GetProductsQuery query, CancellationToken ct = default);

Migration from v1.0.6

If upgrading from v1.0.6 or earlier:

Old:

cache.Remove(cacheKey);  // Manual cache key management

New:

_commandor.Invalidate<IYourService>();  // Service-level invalidation

License

MIT

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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.1.0 1,582 12/30/2025
1.0.6 2,297 12/22/2025
1.0.5 2,289 12/22/2025
1.0.4 2,306 12/22/2025
1.0.3 2,312 12/22/2025
1.0.2 2,317 12/22/2025
1.0.1 2,847 12/15/2025
1.0.0 4,294 11/18/2025

test