Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions samples/EasyWay.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,24 @@ await kernel
//app.UseEasyWay();


app.MapPost("/query", async ([FromBody] SampleQuery query, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>
app.MapPost("/query", async ([FromBody] SampleQuery query, IWebApiModulExecutor<SampleModule> executor) =>
{
var x = await executor.Query<SampleQuery, SampleQueryResult>(query);

return mapper.Map(x);
return await executor.Query<SampleQuery, SampleQueryResult>(query);
});

app.MapPost("/commandWithError", async ([FromBody] ErrorCommand command, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>
app.MapPost("/commandWithError", async ([FromBody] ErrorCommand command, IWebApiModulExecutor<SampleModule> executor) =>
{
var x = await executor.Command(command);

return mapper.Map(x);
return await executor.Command(command);
});

app.MapPost("/command", async ([FromBody] SampleCommand command, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>
app.MapPost("/command", async ([FromBody] SampleCommand command, IWebApiModulExecutor<SampleModule> executor) =>
{
var x = await executor.Command(command);

return mapper.Map(x);
return await executor.Command(command);
});

app.MapPost("/commandwithresult", async ([FromBody] SampleCommandWithResult command, IModuleExecutor<SampleModule> executor, IWebApiResultMapper mapper) =>
app.MapPost("/commandwithresult", async ([FromBody] SampleCommandWithResult command, IWebApiModulExecutor<SampleModule> executor) =>
{
var x = await executor.Command<SampleCommandWithResult, SampleCommandResult>(command);

return mapper.Map(x);
return await executor.Command<SampleCommandWithResult, SampleCommandResult>(command);
});

app.Run();
Expand Down
3 changes: 2 additions & 1 deletion source/EasyWay.WebApi/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public static void AddEasyWayWebApi(
{
services.AddHttpContextAccessor();

services.AddSingleton<IWebApiResultMapper, WebApiResultMapper>();
services.AddSingleton<WebApiResultMapper>();
services.AddSingleton(typeof(IWebApiModulExecutor<>), typeof(WebApiModulExecutor<>));

services.AddScoped<IUserContext, UserContext>();
}
Expand Down
19 changes: 19 additions & 0 deletions source/EasyWay.WebApi/IWebApiModulExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Http;

namespace EasyWay
{
public interface IWebApiModulExecutor<TModule>
where TModule : EasyWayModule
{
Task<IResult> Command<TCommand>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : Command;

Task<IResult> Command<TCommand, TOperationResult>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : Command<TOperationResult>
where TOperationResult : OperationResult;

Task<IResult> Query<TQuery, TReadModel>(TQuery query, CancellationToken cancellationToken = default)
where TQuery : Query<TReadModel>
where TReadModel : ReadModel;
}
}
15 changes: 0 additions & 15 deletions source/EasyWay.WebApi/IWebApiResultMapper.cs

This file was deleted.

46 changes: 46 additions & 0 deletions source/EasyWay.WebApi/Internals/WebApiModulExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Http;

namespace EasyWay.Internals
{
internal sealed class WebApiModulExecutor<TModule> : IWebApiModulExecutor<TModule>
where TModule : EasyWayModule
{
private readonly IModuleExecutor<TModule> _moduleExecutor;

private readonly WebApiResultMapper _mapper;

public WebApiModulExecutor(
IModuleExecutor<TModule> moduleExecutor,
WebApiResultMapper mapper)
{
_moduleExecutor = moduleExecutor;
_mapper = mapper;
}

public async Task<IResult> Command<TCommand>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : Command
{
var commandResult = await _moduleExecutor.Command(command, cancellationToken);

return _mapper.Map(commandResult);
}

public async Task<IResult> Command<TCommand, TOperationResult>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : Command<TOperationResult>
where TOperationResult : OperationResult
{
var commandResult = await _moduleExecutor.Command<TCommand, TOperationResult>(command, cancellationToken);

return _mapper.Map(commandResult);
}

public async Task<IResult> Query<TQuery, TReadModel>(TQuery query, CancellationToken cancellationToken = default)
where TQuery : Query<TReadModel>
where TReadModel : ReadModel
{
var commandResult = await _moduleExecutor.Query<TQuery, TReadModel>(query, cancellationToken);

return _mapper.Map(commandResult);
}
}
}
2 changes: 1 addition & 1 deletion source/EasyWay.WebApi/Internals/WebApiResultMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace EasyWay.Internals
{
internal sealed class WebApiResultMapper : IWebApiResultMapper
internal sealed class WebApiResultMapper
{
public IResult Map(CommandResult commandResult)
{
Expand Down
Loading