Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Davish.Endpoints

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.

Install

dotnet add package Davish.Endpoints

Quick start

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

Nested groups

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.

How it works

  • AddEndpoints() is generated by a source generator that scans your project for non-abstract classes implementing IEndpoint/IEndpoint<TGroup> and IGroupEndpoint/IGroupEndpoint<TParent>, and registers each one into IServiceCollection as 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 calls Configure/AddRoutes in 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.

About

Source-generator-based endpoint discovery and DI registration for ASP.NET Core Minimal APIs — no reflection-based assembly scanning.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages