From cb851a4ec64c78681a7f1f473ac58fa1c4902792 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 27 Nov 2025 10:56:21 +0100 Subject: [PATCH] Refactor RecAction commands and queries to use records Refactored `InvokeRecActionCommand` and `ReadRecActionQuery` to leverage C# `record` types for immutability and improved data structure clarity. Introduced `ReadRecActionQueryBase` as a shared base record to separate common properties and logic. Updated `InvokeRecActionCommandHandler` to use the new `ToReadQuery` method for compatibility. These changes enhance code maintainability, reusability, and separation of concerns while preserving existing functionality. --- .../RecActions/Commands/InvokeRecActionCommand.cs | 6 ++---- .../RecActions/Queries/ReadRecActionQuery.cs | 6 +++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index 6d7acd6..de3dc62 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -4,9 +4,7 @@ using ReC.Application.RecActions.Queries; namespace ReC.Application.RecActions.Commands; -public class InvokeRecActionCommand : ReadRecActionQuery, IRequest -{ -} +public record InvokeRecActionCommand : ReadRecActionQueryBase, IRequest; public static class InvokeRecActionCommandExtensions { @@ -18,7 +16,7 @@ public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory cl { public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel) { - var actions = await sender.Send(request as ReadRecActionQuery, cancel); + var actions = await sender.Send(request.ToReadQuery(), cancel); var http = clientFactory.CreateClient(); diff --git a/src/ReC.Application/RecActions/Queries/ReadRecActionQuery.cs b/src/ReC.Application/RecActions/Queries/ReadRecActionQuery.cs index 7d7c769..f3a1f26 100644 --- a/src/ReC.Application/RecActions/Queries/ReadRecActionQuery.cs +++ b/src/ReC.Application/RecActions/Queries/ReadRecActionQuery.cs @@ -8,11 +8,15 @@ using ReC.Application.Common.Dto; namespace ReC.Application.RecActions.Queries; -public class ReadRecActionQuery : IRequest> +public record ReadRecActionQueryBase { public int ProfileId { get; init; } + + public ReadRecActionQuery ToReadQuery() => new(this); } +public record ReadRecActionQuery(ReadRecActionQueryBase Root) : ReadRecActionQueryBase(Root), IRequest>; + public class ReadRecActionQueryHandler(IRepository repo, IMapper mapper) : IRequestHandler> { public async Task> Handle(ReadRecActionQuery request, CancellationToken cancel)