From 092c1ef2327bfdf4d7b55b18060c74fd43199213 Mon Sep 17 00:00:00 2001 From: adimiko Date: Fri, 14 Mar 2025 21:35:44 +0100 Subject: [PATCH] Added WebApiModulExecutor --- samples/EasyWay.Samples/Program.cs | 24 ++++------ source/EasyWay.WebApi/Extensions.cs | 3 +- source/EasyWay.WebApi/IWebApiModulExecutor.cs | 19 ++++++++ source/EasyWay.WebApi/IWebApiResultMapper.cs | 15 ------ .../Internals/WebApiModulExecutor.cs | 46 +++++++++++++++++++ .../Internals/WebApiResultMapper.cs | 2 +- 6 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 source/EasyWay.WebApi/IWebApiModulExecutor.cs delete mode 100644 source/EasyWay.WebApi/IWebApiResultMapper.cs create mode 100644 source/EasyWay.WebApi/Internals/WebApiModulExecutor.cs diff --git a/samples/EasyWay.Samples/Program.cs b/samples/EasyWay.Samples/Program.cs index 6f16c37..10bc021 100644 --- a/samples/EasyWay.Samples/Program.cs +++ b/samples/EasyWay.Samples/Program.cs @@ -32,32 +32,24 @@ await kernel //app.UseEasyWay(); -app.MapPost("/query", async ([FromBody] SampleQuery query, IModuleExecutor executor, IWebApiResultMapper mapper) => +app.MapPost("/query", async ([FromBody] SampleQuery query, IWebApiModulExecutor executor) => { - var x = await executor.Query(query); - - return mapper.Map(x); + return await executor.Query(query); }); -app.MapPost("/commandWithError", async ([FromBody] ErrorCommand command, IModuleExecutor executor, IWebApiResultMapper mapper) => +app.MapPost("/commandWithError", async ([FromBody] ErrorCommand command, IWebApiModulExecutor executor) => { - var x = await executor.Command(command); - - return mapper.Map(x); + return await executor.Command(command); }); -app.MapPost("/command", async ([FromBody] SampleCommand command, IModuleExecutor executor, IWebApiResultMapper mapper) => +app.MapPost("/command", async ([FromBody] SampleCommand command, IWebApiModulExecutor executor) => { - var x = await executor.Command(command); - - return mapper.Map(x); + return await executor.Command(command); }); -app.MapPost("/commandwithresult", async ([FromBody] SampleCommandWithResult command, IModuleExecutor executor, IWebApiResultMapper mapper) => +app.MapPost("/commandwithresult", async ([FromBody] SampleCommandWithResult command, IWebApiModulExecutor executor) => { - var x = await executor.Command(command); - - return mapper.Map(x); + return await executor.Command(command); }); app.Run(); diff --git a/source/EasyWay.WebApi/Extensions.cs b/source/EasyWay.WebApi/Extensions.cs index 1725877..8b44b06 100644 --- a/source/EasyWay.WebApi/Extensions.cs +++ b/source/EasyWay.WebApi/Extensions.cs @@ -13,7 +13,8 @@ public static void AddEasyWayWebApi( { services.AddHttpContextAccessor(); - services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(typeof(IWebApiModulExecutor<>), typeof(WebApiModulExecutor<>)); services.AddScoped(); } diff --git a/source/EasyWay.WebApi/IWebApiModulExecutor.cs b/source/EasyWay.WebApi/IWebApiModulExecutor.cs new file mode 100644 index 0000000..3819e5a --- /dev/null +++ b/source/EasyWay.WebApi/IWebApiModulExecutor.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Http; + +namespace EasyWay +{ + public interface IWebApiModulExecutor + where TModule : EasyWayModule + { + Task Command(TCommand command, CancellationToken cancellationToken = default) + where TCommand : Command; + + Task Command(TCommand command, CancellationToken cancellationToken = default) + where TCommand : Command + where TOperationResult : OperationResult; + + Task Query(TQuery query, CancellationToken cancellationToken = default) + where TQuery : Query + where TReadModel : ReadModel; + } +} diff --git a/source/EasyWay.WebApi/IWebApiResultMapper.cs b/source/EasyWay.WebApi/IWebApiResultMapper.cs deleted file mode 100644 index a69f96d..0000000 --- a/source/EasyWay.WebApi/IWebApiResultMapper.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.AspNetCore.Http; - -namespace EasyWay -{ - public interface IWebApiResultMapper - { - IResult Map(CommandResult commandResult); - - IResult Map(CommandResult commandResult) - where TOperationResult : OperationResult; - - IResult Map(QueryResult queryResult) - where TReadModel : ReadModel; - } -} diff --git a/source/EasyWay.WebApi/Internals/WebApiModulExecutor.cs b/source/EasyWay.WebApi/Internals/WebApiModulExecutor.cs new file mode 100644 index 0000000..9ac8e7d --- /dev/null +++ b/source/EasyWay.WebApi/Internals/WebApiModulExecutor.cs @@ -0,0 +1,46 @@ +using Microsoft.AspNetCore.Http; + +namespace EasyWay.Internals +{ + internal sealed class WebApiModulExecutor : IWebApiModulExecutor + where TModule : EasyWayModule + { + private readonly IModuleExecutor _moduleExecutor; + + private readonly WebApiResultMapper _mapper; + + public WebApiModulExecutor( + IModuleExecutor moduleExecutor, + WebApiResultMapper mapper) + { + _moduleExecutor = moduleExecutor; + _mapper = mapper; + } + + public async Task Command(TCommand command, CancellationToken cancellationToken = default) + where TCommand : Command + { + var commandResult = await _moduleExecutor.Command(command, cancellationToken); + + return _mapper.Map(commandResult); + } + + public async Task Command(TCommand command, CancellationToken cancellationToken = default) + where TCommand : Command + where TOperationResult : OperationResult + { + var commandResult = await _moduleExecutor.Command(command, cancellationToken); + + return _mapper.Map(commandResult); + } + + public async Task Query(TQuery query, CancellationToken cancellationToken = default) + where TQuery : Query + where TReadModel : ReadModel + { + var commandResult = await _moduleExecutor.Query(query, cancellationToken); + + return _mapper.Map(commandResult); + } + } +} diff --git a/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs b/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs index 0fff876..b31d14c 100644 --- a/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs +++ b/source/EasyWay.WebApi/Internals/WebApiResultMapper.cs @@ -5,7 +5,7 @@ namespace EasyWay.Internals { - internal sealed class WebApiResultMapper : IWebApiResultMapper + internal sealed class WebApiResultMapper { public IResult Map(CommandResult commandResult) {