Allegro.Extensions.Cqrs.FluentValidations
2.1.0
Prefix Reserved
dotnet add package Allegro.Extensions.Cqrs.FluentValidations --version 2.1.0
NuGet\Install-Package Allegro.Extensions.Cqrs.FluentValidations -Version 2.1.0
<PackageReference Include="Allegro.Extensions.Cqrs.FluentValidations" Version="2.1.0" />
paket add Allegro.Extensions.Cqrs.FluentValidations --version 2.1.0
#r "nuget: Allegro.Extensions.Cqrs.FluentValidations, 2.1.0"
// Install Allegro.Extensions.Cqrs.FluentValidations as a Cake Addin #addin nuget:?package=Allegro.Extensions.Cqrs.FluentValidations&version=2.1.0 // Install Allegro.Extensions.Cqrs.FluentValidations as a Cake Tool #tool nuget:?package=Allegro.Extensions.Cqrs.FluentValidations&version=2.1.0
Allegro.Extensions.Cqrs
Believe that there is no need to describe it once more and point you to authority:
https://martinfowler.com/bliki/CQRS.html
In this package our custom implementation of tools and markers are delivered.
Allegro.Extensions.Cqrs.Abstractions
Contains common CQRS set of markers and abstractions like Command
, Query<>
, ICommandDispatcher
, IQueryDispatcher
, ICommandHandler
, IQueryHandler
.
Additionally we introduce some additional things like Command and Query Validators, or Fluent Validations.
ICommandValidator and IQueryValidator
In more sophisticated validation cases, that simple DataAnnotations are not enough we introduce ICommandValidator<ICommand>
and IQueryValidator<IQuery>
to enables adding some validation logic before command or query execution.
internal class BarCommandValidator : ICommandValidator<BarCommand>
{
public Task Validate(BarCommand command)
{
// validation logic
}
}
internal class BarQueryValidator : IQueryValidator<BarQuery>
{
public Task Validate(BarQuery query, CancellationToken cancellationToken)
{
// validation logic
}
}
Fluent validations
You can use AddCqrsFluentValidations
from package Allegro.Extensions.Cqrs.FluentValidations
to use Fluent Validations instead proposed interfaces.
services.AddCqrsFluentValidations(cqrsAssemblies)
internal class FooCommandFluentValidator : AbstractValidator<FooCommand>
{
public FooCommandFluentValidator()
{
RuleFor(_ => _.Name).NotEmpty();
}
}
Decorators
This give opportunity to Decorate handlers with any custom code.
Remember to add Decorator
attribute to your decorator.
Thanks to it, it will be excluded from auto-registration of handlers and does not loop 😉
[Decorator]
internal class BarCommandHandlerDecorator : ICommandHandler<BarCommand>
{
private readonly ICommandHandler<BarCommand> _decorated;
private readonly ILogger<BarCommand> _logger;
public BarCommandHandlerDecorator(ICommandHandler<BarCommand> decorated, ILogger<BarCommand> logger)
{
_decorated = decorated;
_logger = logger;
}
public async Task Handle(BarCommand command)
{
_logger.LogInformation("Before handle");
await _decorated.Handle(command);
_logger.LogInformation("After handle");
}
}
[Decorator]
internal class BarQueryHandlerDecorator : IQueryHandler<BarQuery, BarData>
{
private readonly IQueryHandler<BarQuery, BarData> _decorated;
private readonly ILogger<BarCommand> _logger;
public BarQueryHandlerDecorator(IQueryHandler<BarQuery, BarData> decorated, ILogger<BarCommand> logger)
{
_decorated = decorated;
_logger = logger;
}
public async Task<BarData> Handle(BarQuery query, CancellationToken cancellationToken)
{
_logger.LogInformation("Before handle");
var result = await _decorated.Handle(query, cancellationToken);
_logger.LogInformation("After handle");
return result;
}
}
Registration with Scrutor:
services.TryDecorate<ICommandHandler<BarCommand>, BarCommandHandlerDecorator>();
services.TryDecorate<IQueryHandler<BarQuery, BarData>, BarQueryHandlerDecorator>();
Remember to first register all commands handlers and than register custom decorator. Execution order is LIFO to registration order.
Samples
Some sample usage could be found:
Allegro.Extensions.Cqrs
This package contains:
- default implementation of
ICommandDispatcher
andIQueryDispatcher
- automatic registrations of all
ICommandHandler
,IQueryHandler
,ICommandValidator
For registrations Scrutor packages is used as a tool.
Why not MediatR?
- for learning purposes
- it is simple code, MediatR is still too much
- better separation of queries and commands for decorators (IPipelineBehavior doesn't allow for this)
- Command without return type
- Good article why not: https://cezarypiatek.github.io/post/why-i-dont-use-mediatr-for-cqrs/
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 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. |
-
net6.0
- Allegro.Extensions.Cqrs.Abstractions (>= 2.1.0)
- FluentValidation (>= 11.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.