QuokkaDev.Cqrs.Decorators 1.0.4-alpha1

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

// Install QuokkaDev.Cqrs.Decorators as a Cake Tool
#tool nuget:?package=QuokkaDev.Cqrs.Decorators&version=1.0.4-alpha1&prerelease                

Quality Gate Status Coverage Maintainability Rating Technical Debt Duplicated Lines (%) publish workflow

QuokkaDev.Cqrs.Decorators

QuokkaDev.Cqrs.Decorators is a package containing some decorators for extending ICommandDispather and IQueryDispatcher with some useful cross cutting concerns. Actually 4 decorators are provided:

  • CommandLoggerDecorator
  • QueryLoggerDecorator
  • CommandValidationDecorator
  • QueryValidationDecorator

CommandLoggerDecorator

Allow logging command request and responses

program.cs
builder.Services.AddCQRS(Assembly.GetExecutingAssembly());
builder.Services.AddCommandLogging(opts => { 
    opts.IsResponseLoggingEnabled = true;
    opts.IsResponseLoggingEnabled = true;
});

QueryLoggerDecorator

Allow logging query request and responses

program.cs
builder.Services.AddCQRS(Assembly.GetExecutingAssembly());
builder.Services.AddQueryLogging(opts => { 
    opts.IsResponseLoggingEnabled = true;
    opts.IsResponseLoggingEnabled = true;
});

CommandValidationDecorator

Validate a command using a Validator created with FluentValidation

program.cs
builder.Services.AddCQRS(Assembly.GetExecutingAssembly());
builder.Services.AddCommandValidation();

Please, note that the extension method only register the validation decorator, it is your responsibility register Validators in the Dependency Injection Container

QueryValidationDecorator

Validate a query using a Validator created with FluentValidation

program.cs
builder.Services.AddCQRS(Assembly.GetExecutingAssembly());
builder.Services.AddQueryValidation();

Please, note that the extension method only register the validation decorator, it is your responsibility register Validators in the Dependency Injection Container

Register multiple decorators

You can combine multiple decorators on ICommandDispatcher or IQueryDispatcher using 'And' extensions methods. For example for registering Validation and Logging decorators to IQueryDispatcher try:

program.cs
builder.Services.AddCQRS(Assembly.GetExecutingAssembly());
builder.Services.AddQueryValidation().AndQueryLogging(opts => { 
    opts.IsResponseLoggingEnabled = true;
    opts.IsResponseLoggingEnabled = true;
});

The decorators are executed in reverse order respect to the extensions method calls. In the abbove example the execution order is:

QueryLoggingDecorator --> QueryValidationDecorator --> QueryDispatcher

Create custom decorators

You can create your custom decorator implementing ICommandDispatcher or IQueryDispatcher. For Example:

program.cs
public class MyCustomDecorator : IQueryDispatcher
{
    private readonly IQueryDispatcher dispatcher;    
    public MyCustomDecorator(IQueryDispatcher dispatcher)
    {
        this.dispatcher = dispatcher;        
    }
    public async Task<TQueryResult> Dispatch<TQuery, TQueryResult>(TQuery query, CancellationToken cancellation) where TQuery : IQuery<TQueryResult>
    {
        // Run code before dispatching query
        var result = await dispatcher.Dispatch<TQuery, TQueryResult>(query, cancellation);
        // Run code after query dispatched
        return result
    }
    public Task<TQueryResult> Dispatch<TQuery, TQueryResult>(TQuery query) where TQuery : IQuery<TQueryResult>
    {
        return Dispatch<TQuery, TQueryResult>(query, CancellationToken.None);
    }
}

For register your custom decorator you can decorate the IQueryDispatcher interface. You can do it using Scrutor (QuokkaDev.Cqrs.Decorators use Scrutor in his extensions method)

program.cs
builder.Services.AddCQRS(Assembly.GetExecutingAssembly());
// Register validation and logging
builder.Services.AddQueryValidation().AndQueryLogging(opts => { 
    opts.IsResponseLoggingEnabled = true;
    opts.IsResponseLoggingEnabled = true;
});
// Register MyCustomValidator using Scrutor extensions methods
builder.Services.Decorate<IQueryDispatcher>((inner, provider) => new MyCustomeDecorator(inner));

The result is: MyCustomValidator --> QueryLoggingDecorator --> QueryValidationDecorator --> QueryDispatcher

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 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. 
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.4-alpha1 192 6/11/2022
1.0.3 487 6/10/2022
1.0.2 409 6/10/2022
1.0.1 415 6/1/2022
1.0.1-alpha2 175 6/2/2022
1.0.0 423 5/30/2022
0.0.1 415 5/30/2022