From 2ca85a23722e7e27cc4b3f098f9dc55e9b3397c6 Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 24 Mar 2026 16:01:24 +0100 Subject: [PATCH] Refactor action behaviors to use Unit instead of bool Refactored PreprocessingBehavior and PostprocessingBehavior for InvokeRecActionViewCommand to use MediatR's Unit as the response type instead of bool. Updated method signatures, return values, and DI registration accordingly to align with MediatR conventions for commands without return values. --- .../Common/Behaviors/Action/PostprocessingBehavior.cs | 11 +++++------ .../Common/Behaviors/Action/PreprocessingBehavior.cs | 6 +++--- src/ReC.Application/DependencyInjection.cs | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs b/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs index 88f7d43..222081b 100644 --- a/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs +++ b/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs @@ -7,19 +7,18 @@ using System.Text.Json; namespace ReC.Application.Common.Behaviors.Action; -public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior +public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior { - public async Task Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate next, CancellationToken cancel) + public async Task Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate next, CancellationToken cancel) { - var response = false; try { - response = await next(cancel); + await next(cancel); } catch { if (request.Action.ErrorAction == ErrorAction.Stop) - return false; + return Unit.Value; } string? info = null, error = null; @@ -44,6 +43,6 @@ public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPi Error = error }, cancel); - return response; + return Unit.Value; } } \ No newline at end of file diff --git a/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs b/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs index c0dc973..7383c17 100644 --- a/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs +++ b/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs @@ -7,9 +7,9 @@ using System.Text.Json; namespace ReC.Application.Common.Behaviors.Action; -public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior +public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior { - public async Task Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate next, CancellationToken cancel) + public async Task Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate next, CancellationToken cancel) { string? info = null, error = null; @@ -34,7 +34,7 @@ public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPip }, cancel); if (error is not null && request.Action.ErrorAction == ErrorAction.Stop) - return false; + return Unit.Value; return await next(cancel); } diff --git a/src/ReC.Application/DependencyInjection.cs b/src/ReC.Application/DependencyInjection.cs index b311589..8017276 100644 --- a/src/ReC.Application/DependencyInjection.cs +++ b/src/ReC.Application/DependencyInjection.cs @@ -35,8 +35,8 @@ public static class DependencyInjection cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); cfg.AddOpenBehaviors([typeof(BodyQueryBehavior<,>), typeof(HeaderQueryBehavior<,>)]); cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); - cfg.AddBehavior(typeof(IPipelineBehavior), typeof(PostprocessingBehavior)); - cfg.AddBehavior(typeof(IPipelineBehavior), typeof(PreprocessingBehavior)); + cfg.AddBehavior(typeof(IPipelineBehavior), typeof(PostprocessingBehavior)); + cfg.AddBehavior(typeof(IPipelineBehavior), typeof(PreprocessingBehavior)); cfg.LicenseKey = configOpt.LuckyPennySoftwareLicenseKey; });