Source-generator-based endpoint discovery and registration for ASP.NET Core Minimal APIs.
Instead of reflection-scanning every loaded assembly at startup to find your endpoint classes, Davish.Endpoints uses a Roslyn incremental source generator to find them at compile time and emit a plain AddEndpoints() registration method. Route mapping (including nested route groups) is then wired up once at startup via a small, bounded reflection pass over the already-resolved instances — not an assembly scan.
dotnet add package Davish.EndpointsDefine a group and one or more endpoints that belong to it:
using Davish.Endpoints;
using Microsoft.AspNetCore.Routing;
public class ApiGroup : IGroupEndpoint
{
public RouteGroupBuilder Configure(IEndpointRouteBuilder endpoints)
=> endpoints.MapGroup("api");
}
public class PingEndpoint : IEndpoint<ApiGroup>
{
public void AddRoutes(IEndpointRouteBuilder endpoints)
=> endpoints.MapGet("ping", () => "pong");
}Wire it up in Program.cs:
using Davish.Endpoints;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpoints(); // generated at compile time
var app = builder.Build();
app.MapEndpoints(); // maps groups + endpoints, parents before children
app.Run();GET /api/ping now returns pong.
Groups can be nested by declaring a parent via IGroupEndpoint<TParent>:
public class ApiGroup : IGroupEndpoint
{
public RouteGroupBuilder Configure(IEndpointRouteBuilder endpoints)
=> endpoints.MapGroup("api");
}
public class AuthGroup : IGroupEndpoint<ApiGroup>
{
public RouteGroupBuilder Configure(IEndpointRouteBuilder endpoints)
=> endpoints.MapGroup("auth");
}
public class LoginEndpoint : IEndpoint<AuthGroup>
{
public void AddRoutes(IEndpointRouteBuilder endpoints)
=> endpoints.MapPost("login", () => Results.Ok());
}POST /api/auth/login is mapped automatically, with ApiGroup configured before AuthGroup.
AddEndpoints()is generated by a source generator that scans your project for non-abstract classes implementingIEndpoint/IEndpoint<TGroup>andIGroupEndpoint/IGroupEndpoint<TParent>, and registers each one intoIServiceCollectionas a singleton.MapEndpoints()resolves those registered instances from the DI container, inspects each instance's own generic interface arguments once (a small, fixed-size reflection pass — not an assembly scan) to determine group/parent relationships, and callsConfigure/AddRoutesin dependency order.
This split keeps the expensive part (finding all your endpoint types across a potentially large codebase) at compile time, while keeping the cheap part (wiring a handful of already-resolved instances together) as ordinary, easy-to-read runtime code.