namespace BackOffice.BFF.WebApi.Common.Services; public interface IDispatchRequestToCQRS { Task Handle(TRequest request, ServerCallContext context); Task Handle(TRequest request, ServerCallContext context); Task Handle(ServerCallContext context); } public class DispatchRequestToCQRS : IDispatchRequestToCQRS { private readonly ISender _sender; public DispatchRequestToCQRS(ISender sender) { _sender = sender; } public async Task Handle(TRequest request, ServerCallContext context) { try { if (request is null) { throw new ArgumentNullException(nameof(request)); } var cqrsInput = request.Adapt(); if (cqrsInput is null) { throw new ArgumentNullException(nameof(cqrsInput)); } var output = await _sender.Send(cqrsInput, context.CancellationToken); return (output ?? throw new InvalidOperationException()).Adapt(); } catch (Exception) { throw; } } public async Task Handle(ServerCallContext context) { try { var cqrsInput = Activator.CreateInstance(); if (cqrsInput is null) { throw new ArgumentNullException(nameof(cqrsInput)); } var output = await _sender.Send(cqrsInput, context.CancellationToken); return (output ?? throw new InvalidOperationException()).Adapt(); } catch (Exception) { throw; } } public async Task Handle(TRequest request, ServerCallContext context) { try { if (request is null) { throw new ArgumentNullException(nameof(request)); } var cqrsInput = request.Adapt(); if (cqrsInput is null) { throw new ArgumentNullException(nameof(cqrsInput)); } await _sender.Send(cqrsInput, context.CancellationToken); return new Empty(); } catch (Exception) { throw; } } }