AspNet.KickStarter.CQRS 1.2.2

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package AspNet.KickStarter.CQRS --version 1.2.2                
NuGet\Install-Package AspNet.KickStarter.CQRS -Version 1.2.2                
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="AspNet.KickStarter.CQRS" Version="1.2.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspNet.KickStarter.CQRS --version 1.2.2                
#r "nuget: AspNet.KickStarter.CQRS, 1.2.2"                
#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 AspNet.KickStarter.CQRS as a Cake Addin
#addin nuget:?package=AspNet.KickStarter.CQRS&version=1.2.2

// Install AspNet.KickStarter.CQRS as a Cake Tool
#tool nuget:?package=AspNet.KickStarter.CQRS&version=1.2.2                

AspNet.KickStarter.CQRS

This library provides the following basic interfaces used to implement CQRS with MediatR

  • ICommand and ICommandHandler
  • IQuery and IQueryHandler

These commands and queries rely on Result, Result<T> and Error types from this library, these types support implicit conversions for ease of use.

The Result class provides Switch and Match methods to conditionally perform actions depending on success or error of the result. They also provide access to the Value or Error values.

The Error class supports FluentValidation errors and provides an AsHttpResult method to produce a suitable IResult from a HTTP handler. For example:

var result = await _mediator.Send(new GetSomeQuery());
return result.Match(
    success => Results.Ok(success),
    error => error.AsHttpResult());

If the unsuccessful result contains a ValidationResult then this will return Results.ValidationProblem with the details, otherwise Results.Problem

TracePipelineBehavior

The library provides a generic TracePipelineBehavior class that adds automatic trace activities for any commands or queries. This can be registered as follows:

builder.Services
    .AddMediatR(_ => _.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
    .AddTracePipelineBehavior();

ValidationPipelineBehavior

The library provides a generic ValidationPipelineBehavior class that enables use of FluentValidation for any commands or queries with a corresponding validator class. This can be registered as follows:

builder.Services
    .AddMediatR(_ => _.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
    .AddValidationPipelineBehavior()
    .AddValidatorsFromAssembly(Assembly.GetExecutingAssembly(), includeInternalTypes: true);

Sample Usage

public record GetDoubleQuery(double Value) : IQuery<double>;
internal async Task<IResult> GetDoubleAsync(double value)
{
    var result = await _mediator.Send(new GetDoubleQuery(value));
    return result.Match(
        success => Results.Ok(success),
        error => error.AsHttpResult());
}
internal class GetDoubleQueryValidator : AbstractValidator<GetDoubleQuery>
{
    public GetDoubleQueryValidator()
    {
        RuleFor(_ => _.Value)
            .GreaterThanOrEqualTo(0)
            .LessThanOrEqualTo(10);
    }
}
internal class GetDoubleQueryHandler : IQueryHandler<GetDoubleQuery, double>
{
    public Task<Result<double>> Handle(GetDoubleQuery request, CancellationToken cancellationToken)
    {
        Result<double> result;
        try
        {
            result = 2 * request.Value;
        }
        catch (Exception ex)
        {
            result = ex;
        }
        return Task.FromResult(result);
    }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AspNet.KickStarter.CQRS:

Package Downloads
AspNet.KickStarter

This simplifies the bootstrapping code to run a minimal API with optional AddIn support for Serilog, FluentValidation, OpenTelemetry and Swagger.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.3 107 6/18/2024
1.2.2 234 6/3/2024
1.2.1 62 6/3/2024
1.2.0 63 6/3/2024
1.1.0 414 12/4/2023