Skip to main content

MinimalApiBuilder by

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: MinimalApiBuilder

Reflectionless, source-generated, thin abstraction layer over the ASP.NET Core Minimal APIs interface

Author:

NuGet: https://www.nuget.org/packages/MinimalApiBuilder/

You can find more details at https://github.com/JensDll/MinimalApiBuilder

Source: https://github.com/JensDll/MinimalApiBuilder

Original Readme

note

MinimalApiBuilder

nuget

Reflectionless, source-generated, thin abstraction layer over the ASP.NET Core Minimal APIs interface.

How to Use

Based on the Vertical Slice Architecture with Feature folder. There is one class for every API endpoint. A basic example looks like the following:

using Microsoft.AspNetCore.Mvc;
using MinimalApiBuilder;

public partial class BasicEndpoint : MinimalApiBuilderEndpoint
{
private static string Handle([FromServices] BasicEndpoint endpoint)
{
return "Hello, World!";
}
}

The endpoint class must be partial, inherit from MinimalApiBuilderEndpoint, and have a Handle or HandleAsync method with the containing type passed from dependency injection. The endpoint is mapped through the typical IEndpointRouteBuilder Map<Verb> extension methods:

app.MapGet<BasicEndpoint>("/hello");

The above is functionally equivalent to:

app.MapGet("/hello", static () => "Hello, World!");

This library depends on FluentValidation >= 11. An endpoint can have a validated request object:

public struct BasicRequest
{
public required string Name { get; init; }
}

public partial class BasicRequestEndpoint : MinimalApiBuilderEndpoint
{
private static string Handle([FromServices] BasicRequestEndpoint endpoint,
[AsParameters] BasicRequest request)
{
return $"Hello, {request.Name}!";
}
}

public class BasicRequestValidator : AbstractValidator<BasicRequest>
{
public BasicRequestValidator()
{
RuleFor(static request => request.Name).MinimumLength(2);
}
}
app.MapGet<BasicRequestEndpoint>("/hello/{name}");

The incremental generator will generate code to validate the request object before the handler is called and return a 400 Bad Request response if the validation fails. In Program.cs the below

builder.Services.AddMinimalApiBuilderEndpoints();

needs to be added to register the necessary types with dependency injection.

About

note

Generate Minimal API from classes

How to use

Example (source csproj, source files)

This is the CSharp Project that references MinimalApiBuilder

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.12" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="MinimalApiBuilder" Version="1.3.3" />
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated>
// This is a MinimalApiBuilder source generated file.
// </auto-generated>

#nullable enable

namespace MinimalApiBuilder
{
public static class DependencyInjection
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("MinimalApiBuilder.Generator", "1.3.3.0")]
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddMinimalApiBuilderEndpoints(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
{
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddScoped<global::BasicEndpoint>(services);
return services;
}
}
}

Useful

Download Example (.NET C#)

Share MinimalApiBuilder

https://ignatandrei.github.io/RSCG_Examples/v2/docs/MinimalApiBuilder

aaa

Category "API" has the following generators:

1 immediate.apis

2 Microsoft.Extensions.Configuration.Binder

3 MinimalApiBuilder

4 MinimalApis.Discovery

5 MinimalHelpers.Routing.Analyzers

6 RDG

7 Refit

8 RSCG_WebAPIExports

9 SafeRouting

10 SkinnyControllersCommon

See category

API