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.
This commit is contained in:
2026-03-24 16:01:24 +01:00
parent dbe09cd07b
commit 2ca85a2372
3 changed files with 10 additions and 11 deletions

View File

@@ -7,19 +7,18 @@ using System.Text.Json;
namespace ReC.Application.Common.Behaviors.Action;
public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior<InvokeRecActionViewCommand, bool>
public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior<InvokeRecActionViewCommand, Unit>
{
public async Task<bool> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<bool> next, CancellationToken cancel)
public async Task<Unit> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<Unit> 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;
}
}

View File

@@ -7,9 +7,9 @@ using System.Text.Json;
namespace ReC.Application.Common.Behaviors.Action;
public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior<InvokeRecActionViewCommand, bool>
public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior<InvokeRecActionViewCommand, Unit>
{
public async Task<bool> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<bool> next, CancellationToken cancel)
public async Task<Unit> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<Unit> 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);
}

View File

@@ -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<InvokeRecActionViewCommand, bool>), typeof(PostprocessingBehavior));
cfg.AddBehavior(typeof(IPipelineBehavior<InvokeRecActionViewCommand, bool>), typeof(PreprocessingBehavior));
cfg.AddBehavior(typeof(IPipelineBehavior<InvokeRecActionViewCommand, Unit>), typeof(PostprocessingBehavior));
cfg.AddBehavior(typeof(IPipelineBehavior<InvokeRecActionViewCommand, Unit>), typeof(PreprocessingBehavior));
cfg.LicenseKey = configOpt.LuckyPennySoftwareLicenseKey;
});