ITLab.Template.DevPack.Behaviors
Provides pipeline behaviors for validation and request logging. Built on top of Mediator (martinothamar). MediatR is no longer referenced.
Componentes
| Tipo | Nome | Responsabilidade |
|---|---|---|
| Class | ValidationPipelineBehavior<TRequest,TResponse> | Runs FluentValidation validators before handlers |
| Class | RequestLoggingPipelineBehavior<TRequest,TResponse> | Logs request lifecycle and sanitized error payloads |
| Static Class | DevPackBehaviorsExtensions | DI registration helper (AddDevPackBehaviors) |
Como usar
// In Program.cs, after services.AddMediator(...).
services.AddDevPackBehaviors();
// Or manually, in arbitrary order:
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestLoggingPipelineBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationPipelineBehavior<,>));
AddDevPackBehaviors registers the pair in the order Logging → Validation → Handler. The first behavior registered is the outermost wrapper.
Validator placement convention
Consumidores tipicamente declaram validators ao lado do Command/Query (Application/{Module}/{Feature}/{Verb}{Entity}Validator.cs) e os registram via services.AddValidatorsFromAssembly(typeof(SomeMarker).Assembly). ValidationPipelineBehavior resolve os validators automaticamente e curto-circuita o pipeline com um ValidationError antes do handler rodar.
public sealed class CreateOrderCommandValidator : AbstractValidator<CreateOrderCommand>
{
public CreateOrderCommandValidator()
{
RuleFor(x => x.CustomerId).NotEmpty();
RuleFor(x => x.Total).GreaterThan(0);
}
}
Mudanças vs MediatR (até v1.x do DevPack)
ValueTask<TResponse>em vez deTask<TResponse>no métodoHandle.MessageHandlerDelegate<TMessage, TResponse>substituiRequestHandlerDelegate<TResponse>. A nova assinatura recebe(message, ct)nonext, em vez de apenas().- Constraint
where TRequest : IMessage(doMediator) substitui constraints arbitrários comowhere TRequest : class. - Open generics ainda requerem registro manual no consumidor — o source generator do
Mediatorregistra apenas tipos fechados.
Mudanças na 10.2.0
- Removido
NormalizeDateTimeOffsetBehavior. Com a migração deAuditedEntityparaDateTimeUTC +LocalTimeZone, o behavior perdeu razão de existir. Pipeline passa a ser Logging → Validation. Projetos que precisem de normalização de fuso de usuário em campos de request devem implementar comportamento próprio na camada de aplicação.
O que não fazer
- Do not move domain invariants to pipeline behaviors.
- Do not depend on request logging behavior to persist audit trails.
- Do not assume validation behavior returns
Resultfor non-Resultresponse types. - Do not return
Task<TResponse>from theHandleoverride — the contract isValueTask<TResponse>.
Extensão pelo produto
- Products can add custom behaviors (authorization, multi-tenancy guards, tracing) around these defaults.
- Products can customize logging policies by extending converters used by
RequestLoggingPipelineBehavior.