jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
11.21k stars 1.18k forks source link

Minimal API Support #653

Closed mumby0168 closed 2 years ago

mumby0168 commented 3 years ago

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.

elementh commented 3 years ago

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

mumby0168 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

Kahbazi commented 3 years ago

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();
        });
    }
}
mumby0168 commented 3 years ago

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?

bobbyangers commented 3 years ago

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

Kahbazi commented 3 years ago

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.

srollinet commented 3 years ago

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.

vip32 commented 2 years ago

@srollinet did you make any progress?

srollinet commented 2 years ago

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