question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for argument list surrogates in minimal APIs

See original GitHub issue

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.

The idea here is to support using a type that can act like a surrogate for the argument list in a minimal API. The motivation is about refactoring a minimal API that takes a large set of parameters into one that takes a single object with top level properties that represent what were once arguments. This is a common pattern seen with CQRS frameworks where a request object is made to bind all inputs.

Describe the solution you’d like

app.MapGet("/products/{id}", (int id, int pageSize, int page, ILogger<Program> logger, MyDb db) =>
{
    logger.LogInformation("Getting products for page {Page}, page);
    
    return db.Products.Skip((page - 1) * pageSize).Take(pageSize);
});

The user wishes to refactor this to:

app.MapGet("/products/{id}", ([Parameters]ProductRequest req) =>
{
     req.Logger.LogInformation("Getting products for page {Page}, req.Page);
    
     return req.Db.Products.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize);
});

public record ProductRequest(int Id, int PageSize, int Page, ILogger<Program> Logger, MyDb Db);

The parameter or type needs to be marked explicitly to identify that this is not from the body or elsewhere (query etc). These properties should also support binding attributes:


public class ProductRequest
{
  [FromRoute]
  public int Id { get; set; } 

  [FromQuery]
  public int PageSize { get; set; }

  [FromQuery]
  public int Page { get; set; } 

  [FromServices]
  public ILogger<Program> Logger { get; set; } 

  [FromServices]
  public MyDb Db {get; set; }
}

This would only work for top level properties in this object.

Additional context

Open questions:

  • Do we want to support multiple of these? No 😄

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
brunolins16commented, May 9, 2022

Some additional name ideas:

[AsParameters] [Flatten] [PropertiesAsParameters] [ParameterList] [Surrogate]

0reactions
JamesNKcommented, Jun 18, 2022

Issue for adding support to MVC: https://github.com/dotnet/aspnetcore/issues/42271

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parameter binding in Minimal API applications
Parameter binding is the process of converting request data into strongly typed parameters that are expressed by route handlers.
Read more >
Minimal APIs quick reference
Provides an overview of minimal APIs in ASP.NET Core.
Read more >
API — Click Documentation (8.1.x)
All keyword arguments are forwarded to the underlying command class. For the params argument, any decorated params are appended to the end of...
Read more >
How to use parameter binding in minimal APIs in ASP.NET ...
NET Core 7 is its support for parameter binding in minimal APIs. The goal of this post is to give you a head...
Read more >
Using ASP.NET Core 7 Minimal APIs: Request Filters ... - InfoQ
Essentially, this service represents a TODO list where we can read, add, modify, and delete the items. The items are stored in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found