Add MediatR integration and update IMediatorService interface
This commit is contained in:
parent
cc5ed32cad
commit
96018137cc
@ -2,6 +2,8 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Baguette.Extensions.AspNetCore.Mediator", "src\Baguette.Extensions.AspNetCore.Mediator\Baguette.Extensions.AspNetCore.Mediator.csproj", "{9C7970CB-13DB-4016-A9D4-B52C8788BFBC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Baguette.Extensions.AspNetCore.Mediator.MediatR", "src\Baguette.Extensions.AspNetCore.Mediator.MediatR\Baguette.Extensions.AspNetCore.Mediator.MediatR.csproj", "{2A0764B1-6C7D-4729-8A10-071897166AE4}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -12,5 +14,9 @@ Global
|
||||
{9C7970CB-13DB-4016-A9D4-B52C8788BFBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9C7970CB-13DB-4016-A9D4-B52C8788BFBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9C7970CB-13DB-4016-A9D4-B52C8788BFBC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2A0764B1-6C7D-4729-8A10-071897166AE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2A0764B1-6C7D-4729-8A10-071897166AE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2A0764B1-6C7D-4729-8A10-071897166AE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2A0764B1-6C7D-4729-8A10-071897166AE4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Baguette.Extensions.AspNetCore.Mediator\Baguette.Extensions.AspNetCore.Mediator.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MediatR" Version="13.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,27 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using MediatR;
|
||||
|
||||
namespace Baguette.Extensions.AspNetCore.Mediator.MediatR;
|
||||
|
||||
public class MediatRService(ISender sender) : IMediatorService
|
||||
{
|
||||
public async ValueTask<bool> TrySendAsync(object? value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (value is not IRequest request)
|
||||
return false;
|
||||
|
||||
await sender.Send(request, cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async ValueTask<(bool IsSucess, TResponse? Response)> TrySendAsync<TResponse>(object? value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (value is not IRequest<TResponse> request)
|
||||
return (false, default);
|
||||
|
||||
var response = await sender.Send(request, cancellationToken);
|
||||
|
||||
return (true, response);
|
||||
}
|
||||
}
|
||||
@ -4,11 +4,7 @@ namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||
|
||||
public interface IMediatorService
|
||||
{
|
||||
bool TryGetRequest(object? value, [NotNullWhen(true)] out IRequest? request);
|
||||
ValueTask<bool> TrySendAsync(object? value, CancellationToken cancellationToken = default);
|
||||
|
||||
bool TryGetRequest<TResponse>(object? value, [NotNullWhen(true)] out IRequest<TResponse>? request);
|
||||
|
||||
ValueTask SendAsync(IRequest request, CancellationToken cancellationToken = default);
|
||||
|
||||
ValueTask<TResponse> SendAsync<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
|
||||
ValueTask<(bool IsSucess, TResponse? Response)> TrySendAsync<TResponse>(object? value, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||
|
||||
public interface IRequest<out TResponse>;
|
||||
|
||||
public interface IRequest;
|
||||
@ -13,14 +13,13 @@ public class SendEndpointRequestFilter<TResponse> : IEndpointFilter
|
||||
var value = await next(context);
|
||||
|
||||
if (value is IResult)
|
||||
return value;
|
||||
return value; // If the next delegate already returned an IResult, just return it
|
||||
|
||||
if (!mediator.TryGetRequest<TResponse>(value, out var typedRequest))
|
||||
throw new InvalidOperationException(); // TODO: Handle case where the request is not an IRequest
|
||||
var (success, response) = await mediator.TrySendAsync<TResponse>(value);
|
||||
|
||||
var response = await mediator.SendAsync(typedRequest, context.HttpContext.RequestAborted);
|
||||
|
||||
return response;
|
||||
return success
|
||||
? Results.Ok(response)
|
||||
: throw new InvalidOperationException(); // TODO: Handle case where request can't be sent or response can't be produced
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,14 +32,11 @@ public class SendEndpointRequestFilter : IEndpointFilter
|
||||
var value = await next(context);
|
||||
|
||||
if (value is IResult)
|
||||
return value;
|
||||
return value; // If the next delegate already returned an IResult, just return it
|
||||
|
||||
if (!mediator.TryGetRequest(value, out var typedRequest))
|
||||
throw new InvalidOperationException(); // TODO: Handle case where the request is not an IRequest
|
||||
|
||||
await mediator.SendAsync(typedRequest, context.HttpContext.RequestAborted);
|
||||
|
||||
return null;
|
||||
return await mediator.TrySendAsync(value)
|
||||
? Results.Ok()
|
||||
: throw new InvalidOperationException(); // TODO: Handle case where request can't be sent
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user