MediatR icon indicating copy to clipboard operation
MediatR copied to clipboard

Minimal API Support

Open mumby0168 opened this issue 3 years ago • 11 comments

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.

mumby0168 avatar Aug 12 '21 10:08 mumby0168

That would be very cool, but my opinion is that it should be in the form of an extension nuget package, maybe?

elementh avatar Aug 12 '21 18:08 elementh

Yeah something like MediatR.Extensions.AspNetCore.Http ?

I would love to get cracking with this just not sure what repository it would live under?

mumby0168 avatar Aug 12 '21 18:08 mumby0168

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

mumby0168 avatar Aug 13 '21 08:08 mumby0168

@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.

mumby0168 avatar Aug 20 '21 19:08 mumby0168

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();
        });
    }
}

Kahbazi avatar Sep 06 '21 11:09 Kahbazi

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?

mumby0168 avatar Sep 06 '21 11:09 mumby0168

Yeah that us pretty cool. And how would you apply security requirements, on the IRequest<T> with a custom filter ?

bobbyangers avatar Sep 06 '21 13:09 bobbyangers

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.

Kahbazi avatar Sep 06 '21 16:09 Kahbazi

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 avatar Oct 02 '21 18:10 srollinet

@srollinet did you make any progress?

vip32 avatar Jul 27 '22 13:07 vip32

@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/

srollinet avatar Aug 08 '22 09:08 srollinet