Pular para o conteúdo principal
Versão: main (dev) 🚧

ITLab.Template.DevPack.Behaviors

Provides pipeline behaviors for validation and request logging. Built on top of Mediator (martinothamar). MediatR is no longer referenced.

Componentes

TipoNomeResponsabilidade
ClassValidationPipelineBehavior<TRequest,TResponse>Runs FluentValidation validators before handlers
ClassRequestLoggingPipelineBehavior<TRequest,TResponse>Logs request lifecycle and sanitized error payloads
Static ClassDevPackBehaviorsExtensionsDI 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 de Task<TResponse> no método Handle.
  • MessageHandlerDelegate<TMessage, TResponse> substitui RequestHandlerDelegate<TResponse>. A nova assinatura recebe (message, ct) no next, em vez de apenas ().
  • Constraint where TRequest : IMessage (do Mediator) substitui constraints arbitrários como where TRequest : class.
  • Open generics ainda requerem registro manual no consumidor — o source generator do Mediator registra apenas tipos fechados.

Mudanças na 10.2.0

  • Removido NormalizeDateTimeOffsetBehavior. Com a migração de AuditedEntity para DateTime UTC + 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 Result for non-Result response types.
  • Do not return Task<TResponse> from the Handle override — the contract is ValueTask<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.