CommandQuery 4.0.0

dotnet add package CommandQuery --version 4.0.0                
NuGet\Install-Package CommandQuery -Version 4.0.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="CommandQuery" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CommandQuery --version 4.0.0                
#r "nuget: CommandQuery, 4.0.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.
// Install CommandQuery as a Cake Addin
#addin nuget:?package=CommandQuery&version=4.0.0

// Install CommandQuery as a Cake Tool
#tool nuget:?package=CommandQuery&version=4.0.0                

CommandQuery ⚙️

build CodeFactor

Command Query Separation for .NET and C#

Commands

Commands change the state of a system but [traditionally] do not return a value. They write (create, update, delete) data.

Commands implement the marker interface ICommand and command handlers implement ICommandHandler<in TCommand>.

public class FooCommand : ICommand
{
    public string Value { get; set; }
}

public class FooCommandHandler : ICommandHandler<FooCommand>
{
    private readonly ICultureService _cultureService;

    public FooCommandHandler(ICultureService cultureService)
    {
        _cultureService = cultureService;
    }

    public async Task HandleAsync(FooCommand command, CancellationToken cancellationToken)
    {
        if (string.IsNullOrEmpty(command.Value)) throw new FooCommandException("Value cannot be null or empty", 1337, "Try setting the value to 'en-US'");

        _cultureService.SetCurrentCulture(command.Value);

        await Task.CompletedTask;
    }
}

Commands can also return a result.

public class BazCommand : ICommand<Baz>
{
    public string Value { get; set; }
}

public class Baz
{
    public bool Success { get; set; }
}

public class BazCommandHandler : ICommandHandler<BazCommand, Baz>
{
    private readonly ICultureService _cultureService;

    public BazCommandHandler(ICultureService cultureService)
    {
        _cultureService = cultureService;
    }

    public async Task<Baz> HandleAsync(BazCommand command, CancellationToken cancellationToken)
    {
        var result = new Baz();

        try
        {
            _cultureService.SetCurrentCulture(command.Value);

            result.Success = true;
        }
        catch
        {
            // TODO: log
        }

        return await Task.FromResult(result);
    }
}

Commands with result implement the marker interface ICommand<TResult> and command handlers implement ICommandHandler<in TCommand, TResult>.

Queries

Queries return a result and do not change the observable state of the system (are free of side effects). They read and return data.

Queries implement the marker interface IQuery<TResult> and query handlers implement IQueryHandler<in TQuery, TResult>.

public class BarQuery : IQuery<Bar>
{
    public int Id { get; set; }
}

public class Bar
{
    public int Id { get; set; }

    public string Value { get; set; }
}

public class BarQueryHandler : IQueryHandler<BarQuery, Bar>
{
    private readonly IDateTimeProxy _dateTime;

    public BarQueryHandler(IDateTimeProxy dateTime)
    {
        _dateTime = dateTime;
    }

    public async Task<Bar> HandleAsync(BarQuery query, CancellationToken cancellationToken)
    {
        var result = new Bar { Id = query.Id, Value = _dateTime.Now.ToString("F") };

        return await Task.FromResult(result);
    }
}

Samples

Product 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 was computed.  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. 
.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 was computed. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on CommandQuery:

Package Downloads
CommandQuery.AspNetCore

Command Query Separation for ASP.NET Core 🌐 ✔️ Provides generic actions for handling the execution of commands and queries ✔️ Enables APIs based on HTTP POST and GET 📄 https://hlaueriksson.me/CommandQuery.AspNetCore/

CommandQuery.AzureFunctions

Command Query Separation for Azure Functions ⚡ ✔️ Provides generic function support for commands and queries with HTTPTriggers ✔️ Enables APIs based on HTTP POST and GET 📄 https://hlaueriksson.me/CommandQuery.AzureFunctions/

CommandQuery.AWSLambda

Command Query Separation for AWS Lambda ⚡ ✔️ Provides generic function support for commands and queries with Amazon API Gateway ✔️ Enables APIs based on HTTP POST and GET 📄 https://hlaueriksson.me/CommandQuery.AWSLambda/

CommandQuery.AspNet.WebApi

Command Query Separation for ASP.NET Web API 2 🌐 ✔️ Provides generic actions for handling the execution of commands and queries ✔️ Enables APIs based on HTTP POST and GET 📄 https://hlaueriksson.me/CommandQuery.AspNet.WebApi/

CommandQuery.SystemTextJson

System.Text.Json extensions for CommandQuery

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on CommandQuery:

Repository Stars
hlaueriksson/CommandQuery
Command Query Separation for 🌐ASP.NET Core ⚡AWS Lambda ⚡Azure Functions ⚡Google Cloud Functions
Version Downloads Last updated
4.0.0 321 7/13/2024
3.0.0 1,656 1/9/2023
2.0.0 1,629 7/29/2021
1.0.0 5,109 2/2/2020
0.9.0 2,606 11/20/2019
0.8.0 5,009 2/16/2019
0.7.0 1,906 9/22/2018
0.6.0 1,866 9/15/2018
0.5.0 1,948 7/6/2018
0.4.0 1,689 5/16/2018
0.3.2 1,796 5/1/2018
0.3.1 1,773 1/6/2018
0.3.0 1,746 1/3/2018
0.2.0 1,867 4/25/2017
0.1.1 1,536 8/29/2016
0.1.0 1,340 8/28/2016

- Bump Microsoft.Extensions.DependencyInjection to 8.0.0