Compare commits
5 Commits
main
...
feature/me
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37cb52fe3e | ||
|
|
544d23a397 | ||
|
|
96018137cc | ||
|
|
cc5ed32cad | ||
|
|
94d4e1352f |
@ -1,8 +1,22 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
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
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{9C7970CB-13DB-4016-A9D4-B52C8788BFBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{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
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||||
|
|
||||||
|
public interface IMediatorService
|
||||||
|
{
|
||||||
|
ValueTask<bool> TrySendAsync(object? value, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
ValueTask<(bool IsSucess, TResponse? Response)> TrySendAsync<TResponse>(object? value, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||||
|
|
||||||
|
public static class MediatorEndpointRouteBuilderExtensions
|
||||||
|
{
|
||||||
|
public static RouteHandlerBuilder SendRequest<TResponse>(this RouteHandlerBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.AddEndpointFilter<SendEndpointRequestFilter<TResponse>>().Produces<TResponse>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RouteHandlerBuilder SendRequest(this RouteHandlerBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.AddEndpointFilter<SendEndpointRequestFilter>().Produces(StatusCodes.Status200OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||||
|
|
||||||
|
public class MediatorOptions
|
||||||
|
{
|
||||||
|
public Func<EndpointFilterInvocationContext, object?, ValueTask<IResult>> OnRequestSendFailed { get; set; } = (_, _) => throw new NotImplementedException();
|
||||||
|
}
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||||
|
|
||||||
|
public class SendEndpointRequestFilter<TResponse> : IEndpointFilter
|
||||||
|
{
|
||||||
|
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||||
|
{
|
||||||
|
var mediator = context.HttpContext.RequestServices.GetRequiredService<IMediatorService>();
|
||||||
|
var options = context.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<MediatorOptions>>().Value;
|
||||||
|
|
||||||
|
var value = await next(context);
|
||||||
|
|
||||||
|
if (value is IResult)
|
||||||
|
return value; // If the next delegate already returned an IResult, just return it
|
||||||
|
|
||||||
|
var (success, response) = await mediator.TrySendAsync<TResponse>(value);
|
||||||
|
|
||||||
|
return success
|
||||||
|
? Results.Ok(response)
|
||||||
|
: await options.OnRequestSendFailed(context, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SendEndpointRequestFilter : IEndpointFilter
|
||||||
|
{
|
||||||
|
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||||
|
{
|
||||||
|
var mediator = context.HttpContext.RequestServices.GetRequiredService<IMediatorService>();
|
||||||
|
var options = context.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<MediatorOptions>>().Value;
|
||||||
|
|
||||||
|
var value = await next(context);
|
||||||
|
|
||||||
|
if (value is IResult)
|
||||||
|
return value; // If the next delegate already returned an IResult, just return it
|
||||||
|
|
||||||
|
return await mediator.TrySendAsync(value)
|
||||||
|
? Results.Ok()
|
||||||
|
: await options.OnRequestSendFailed(context, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user