SimpleCqrsMediator 1.0.0

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

SimpleCqrsMediator

This is a simple implementation of the CqrsMediator patern. It is designed to be easy to use and understand, while still providing the basic functionality needed for a CQRS application.

This project is inspired by project MediatR. The main differance is thats this project is making a distinction between commands and queries. Commands are used to change the state of the system, while queries are used to retrieve data from the system.

features

  • Interface based commands and queries.
  • Automatic ID container registration of command-/ query handlers.
  • Injectable Exception handler.

Usage

Below is a simple example of how to use the CqrsMediator.

First Add the CqrsMediator to your service collection in the Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    var assemblies = new[]
    {
        Assembly.GetExecutingAssembly(),
        Assembly.Load("App.Business"),
        Assembly.Load("App.Data"),
    };

    services.AddCqrsMediator(assemblies);
}

Then you can create a query or command by implementing the IQuery<TResult> or ICommand interface. For example:

public class TestQuery : IQuery<TestResultDto>
{
}

public class TestResultDto
{
    public string Result { get; set; }
}

Next, you need to create a handler for the query or command by implementing the IQueryHandler<TQuery, TResult> or ICommandHandler<TCommand> interface:

public class TestQueryHandler(
    HttpClient HttpClient
    ) : IQueryHandler<TestQuery, TestResultDto>
{
    public async Task<TestResultDto> HandleAsync(TestQuery query)
    {
        var result = await ...// Call your API or perform some logic here
        return new TestResultDto { Result = result };
    }
}

Finally, you can use the CqrsMediator to send the query or command and get the result:

[ApiController]
[Route("[controller]")]
public class RestFullTestController(
    IQueryProcessor QueryProcessor
    ) : ControllerBase
{
    [HttpGet]
    [Route("/Test")]
    public async Task<ActionResult<TestResultDto>> Test(TestQuery query)
    {
        var result = await QueryProcessor.ProcessAsync(query);
        return Ok(result);
    }
}

Exception Handling

If you want to handle exceptions that occur during the processing of commands or queries, you can implement the IProcessorExceptionHandler interface:

public class CustomProcessorExceptionHandler : IProcessorExceptionHandler
{
    public void HandleAsync(Exception exception)
    {
        // Log the exception or perform some custom logic
    }
}

Then, register the exception handler in your service collection:

public void ConfigureServices(IServiceCollection services)
{
    var assemblies = new[]
    {
        Assembly.GetExecutingAssembly(),
        Assembly.Load("App.Business"),
        Assembly.Load("App.Data"),
    };

    services.AddCqrsMediator(assemblies);
    services.AddCqrsMediatorExceptionHandler<CustomProcessorExceptionHandler>();
}

Notes

This is an expirimental project; use in production at your own risk.

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.  net9.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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

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 217 6/9/2025