MediatR
MediatR copied to clipboard
Minimal API Support
Seeing the recent advancements on the minimal APIs within ASP.NET Core.
It would be really cool if we could look at some extension methods that sit on top of this and provide a way to map IRequest<T>
handlers directly to http routes without having to use a controller.
I expect this would be a set of extension methods.
That would be very cool, but my opinion is that it should be in the form of an extension nuget package, maybe?
Yeah something like MediatR.Extensions.AspNetCore.Http
?
I would love to get cracking with this just not sure what repository it would live under?
I started to put together a project to scope this out you can see it here https://github.com/mumby0168/blog-samples/tree/main/new-features/MinimalApis
While doing so I came across a current limitation of the minimal API's I have logged in issue about it here: https://github.com/dotnet/aspnetcore/issues/35304
@jbogard what do you think about this? They have recently added a more flexible way to allow custom parameters to be bound. See the previous comment with a link to the issue.
Yeah something like MediatR.Extensions.AspNetCore.Http ?
@mumby0168 This is what I have developed in https://github.com/Kahbazi/MediatR.AspNetCore.Endpoints
You can register IRequest<T>
directly to the ASP.NET Core routing without Controllers.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMediatR(GetType().Assembly);
services.AddMediatREndpoints();
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapMediatR();
});
}
}
Yeah something like MediatR.Extensions.AspNetCore.Http ?
@mumby0168 This is what I have developed in https://github.com/Kahbazi/MediatR.AspNetCore.Endpoints
You can register
IRequest<T>
directly to the ASP.NET Core routing without Controllers.public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMediatR(GetType().Assembly); services.AddMediatREndpoints(); } public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapMediatR(); }); } }
Ah that is really cool, how do you handle mapping query params to an object?
Yeah that us pretty cool. And how would you apply security requirements, on the IRequest<T> with a custom filter ?
Ah that is really cool, how do you handle mapping query params to an object?
I haven't implement that yet.
Yeah that us pretty cool. And how would you apply security requirements, on the IRequest with a custom filter ?
You can apply [Authorize]
attributes the same way as controllers and it will work as long as you also add authorization middleware.
I try to do something similar, with extension methods that allow to map a route to a an IRequest.
Something like that
app.MediatR().MapGet("/hello", (string name) => new HelloQuery(name));
Which will be the equivalent of
app.MapGet("/hello", (string name, IMediator mediator, CancellationToken token) =>
mediator.Send(new HelloQuery(name), token)
);
The idea is to have something similar as the original API, with less boilerplate.
The problem is that I don't know the correct approach to use... I tried to create a dynamic delegate that wrap the original one and that adds the mediator and the token, but I don't think it is a good approach, and I have a lack of knowledge in this domain so I struggled with lot of problems.
Maybe the most simple solution is to have a middleware that handles the IRequest, but with this solution, I don't know how Swagger will know the actual return type of the method. I will try to dig a bit.
@srollinet did you make any progress?
@vip32 No, I haven't done anything. And I will probably wait for asp.net core 7 because the new [AsParameters]
attribute will simplify a lot what I want to achieve
https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-preview-5/