AspNet.KickStarter.CQRS
1.2.1
Prefix Reserved
See the version list below for details.
dotnet add package AspNet.KickStarter.CQRS --version 1.2.1
NuGet\Install-Package AspNet.KickStarter.CQRS -Version 1.2.1
<PackageReference Include="AspNet.KickStarter.CQRS" Version="1.2.1" />
paket add AspNet.KickStarter.CQRS --version 1.2.1
#r "nuget: AspNet.KickStarter.CQRS, 1.2.1"
// Install AspNet.KickStarter.CQRS as a Cake Addin #addin nuget:?package=AspNet.KickStarter.CQRS&version=1.2.1 // Install AspNet.KickStarter.CQRS as a Cake Tool #tool nuget:?package=AspNet.KickStarter.CQRS&version=1.2.1
AspNet.KickStarter.CQRS
This library provides the following basic interfaces used to implement CQRS with MediatR
ICommand
andICommandHandler
IQuery
andIQueryHandler
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
OpenTelemetryTracePipelineBehavior
The library also provides a generic OpenTelemetryTracePipelineBehavior
class that adds automatic trace activities for any commands or queries. This can be registered as follows:
builder.Services
.AddMediatR(_ => _.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()))
.AddScoped(typeof(IPipelineBehavior<,>), typeof(OpenTelemetryTracePipelineBehavior<,>));
ValidationPipelineBehavior
The library also 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()))
.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationPipelineBehavior<,>))
.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 | Versions 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. |
-
net6.0
- AspNet.KickStarter.Tracing (>= 1.2.1)
- FluentValidation (>= 11.9.1)
- MediatR (>= 12.2.0)
-
net8.0
- AspNet.KickStarter.Tracing (>= 1.2.1)
- FluentValidation (>= 11.9.1)
- MediatR (>= 12.2.0)
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.