Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Route Groups] Investigate callback based API #41432

Open
1 task done
halter73 opened this issue Apr 28, 2022 · 2 comments
Open
1 task done

[Route Groups] Investigate callback based API #41432

halter73 opened this issue Apr 28, 2022 · 2 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Milestone

Comments

@halter73
Copy link
Member

halter73 commented Apr 28, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

A callback-based version of MapGroup was considered as an [alternative design in the original route grouping issue. In this design, the IEndpointRouteBuilder would be passed to a callback rather than returned from MapGroup.

Describe the solution you'd like

If we use callbacks instead of the return type of MapGroup to define endpoints, it naturally leads to a nested structure in the cod that matches the nested structure of a group. For example:

var builder = WebApplication.CreateBuilder(args);
// ...
var app = builder.Build();

app.MapGroup("/todos", group =>
{
    group.MapGet("/", (int id, TodoDb db) => db.ToListAsync());
    group.MapGet("/{id}", (int id, TodoDb db) => db.GetAsync(id));
    group.MapPost("/", (Todo todo, TodoDb db) => db.AddAsync(todo));

    // string org cannot be an argument to the configureGroup callback because that would require MapGet and other
    // IEndpointRouteBuilder extension methods to be repeatedly called fore every request.
    group.MapGroup("/{org}", nestedGroup =>
    {
        nestedGroup.MapGet("/", (string org, TodoDb db) => db.Filter(todo => todo.Org == org).ToListAsync());
        // ...
    }).RequireAuthorization();
    // ....
}).RequireCors("AllowAll");
// ...

So instead of what we have today:

var builder = WebApplication.CreateBuilder(args);
// ...
var app = builder.Build();

var group = app.MapGroup("/todos");
group.MapGet("/", (int id, TodoDb db) => db.ToListAsync());
group.MapGet("/{id}", (int id, TodoDb db) => db.GetAsync(id));
group.MapPost("/", (Todo todo, TodoDb db) => db.AddAsync(todo));
group.RequireCors("AllowAll");

var nestedGroup = group.MapGroup("/{org}");
nestedGroup.MapGet("/", (string org, TodoDb db) => db.Filter(todo => todo.Org == org).ToListAsync());
nestedGroup.RequireAuthorization();
// ...

You an already manually introduce scopes to get a similar kind of structure with the existing API as demonstrated by https://twitter.com/davidfowl/status/1519480212060139521, but I doubt many people will structure their code like this in practice unless we use callbacks:

var builder = WebApplication.CreateBuilder(args);
// ...
var app = builder.Build();

var group = app.MapGroup("/todos");
{
        group.MapGet("/", (int id, TodoDb db) => db.ToListAsync());
        group.MapGet("/{id}", (int id, TodoDb db) => db.GetAsync(id));
        group.MapPost("/", (Todo todo, TodoDb db) => db.AddAsync(todo));
        group.RequireCors("AllowAll");

        var nestedGroup = group.MapGroup("/{org}");
        {
            nestedGroup.MapGet("/", (string org, TodoDb db) => db.Filter(todo => todo.Org == org).ToListAsync());
            nestedGroup.RequireAuthorization();
        }
}
// ...

Additional context

The current API and a callback-based API are not mutually exclusive. These are different overloads, so both could be supported. I don't like this however, because I think having too many ways to do things does more harm by creating confusion than it helps by providing more flexibility.

If we do decide to support both, we'd need to decide would MapGroup still return a GroupRouteBuilder that you can add endpoints on? And would the callback parameter also be a GroupRouteBuilder meaning you could add both routes and conventions both inside and outside the callback? In our docs, would we normally add endpoints inside the callback and conventions outside the callback like in the sample above?

@halter73 halter73 added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Apr 28, 2022
@halter73 halter73 mentioned this issue Apr 28, 2022
4 tasks
@halter73 halter73 added this to the .NET 7 Planning milestone May 3, 2022
@ghost
Copy link

ghost commented May 3, 2022

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@ghost
Copy link

ghost commented Nov 1, 2022

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@captainsafia captainsafia added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jun 6, 2023
@captainsafia captainsafia modified the milestones: .NET 8 Planning, Backlog Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Projects
None yet
Development

No branches or pull requests

3 participants