From dc777a04f6499539f96149d902d43bb395ccaf2d Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 1 Dec 2025 12:30:24 +0100 Subject: [PATCH] Refactor InvokeRecActionCommand to use composition Refactored `InvokeRecActionCommand` to remove inheritance from `RecActionDto` and introduced a new `Action` property to encapsulate the data. Updated the `ToInvokeCommand` extension method to align with this change. Modified `InvokeRecActionCommandHandler` to reference the `Action` property instead of directly accessing inherited properties. Updated HTTP request creation logic and the `CreateOutResCommand` instantiation to use the `Action` object. This change improves code clarity and adheres to composition over inheritance principles. --- .../Commands/InvokeRecActionCommand.cs | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index 4738e72..5cfa8e5 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -8,16 +8,14 @@ using System.Text.Json; namespace ReC.Application.RecActions.Commands; -public record InvokeRecActionCommand : RecActionDto, IRequest +public record InvokeRecActionCommand : IRequest { - public InvokeRecActionCommand(RecActionDto root) : base(root) { } - - public InvokeRecActionCommand() { } + public RecActionDto Action { get; set; } } public static class InvokeRecActionCommandExtensions { - public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new(dto); + public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new() { Action = dto }; } public class InvokeRecActionCommandHandler( @@ -29,30 +27,31 @@ public class InvokeRecActionCommandHandler( { public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel) { + var action = request.Action; using var http = clientFactory.CreateClient(); - if (request.RestType is null) + if (action.RestType is null) { logger?.LogWarning( "Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}", - request.ProfileId, - request.ActionId + action.ProfileId, + action.ActionId ); return; } - using var httpReq = request.RestType + using var httpReq = action.RestType .ToHttpMethod() - .ToHttpRequestMessage(request.EndpointUri); + .ToHttpRequestMessage(action.EndpointUri); - if(request.Body is not null) + if(action.Body is not null) { - using var reqBody = new StringContent(request.Body); + using var reqBody = new StringContent(action.Body); httpReq.Content = reqBody; } - if (request.Headers is not null) - foreach (var header in request.Headers) + if (action.Headers is not null) + foreach (var header in action.Headers) httpReq.Headers.Add(header.Key, header.Value); using var response = await http.SendAsync(httpReq, cancel); @@ -61,7 +60,7 @@ public class InvokeRecActionCommandHandler( await sender.Send(new CreateOutResCommand { - ActionId = request.ActionId, + ActionId = action.ActionId, Header = JsonSerializer.Serialize(resHeaders, options: new() { WriteIndented = false }), Body = resBody, AddedWho = config?["AddedWho"]