MinimalApiBuilder by
Nuget / site data
Details
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
Original Readme
MinimalApiBuilder
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
Generate Minimal API from classes
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- BasicEndpoint.cs
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>
This is the use of MinimalApiBuilder in Program.cs
using MinimalApiBuilder;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//MinimalApiBuilder.DependencyInjection.AddMinimalApiBuilderEndpoints(builder.Services);
builder.Services.AddMinimalApiBuilderEndpoints();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.MapGet<BasicEndpoint>("/hello");
app.Run();
This is the use of MinimalApiBuilder in BasicEndpoint.cs
using Microsoft.AspNetCore.Mvc;
using MinimalApiBuilder;
public partial class BasicEndpoint : MinimalApiBuilderEndpoint
{
private static string Handle([FromServices] BasicEndpoint endpoint)
{
return "Hello, World!";
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- DependencyInjection.g.cs
- Endpoint.g.cs
// <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;
}
}
}
// <auto-generated>
// This is a MinimalApiBuilder source generated file.
// </auto-generated>
#nullable enable
public partial class BasicEndpoint : global::MinimalApiBuilder.IEndpoint
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("MinimalApiBuilder.Generator", "1.3.3.0")]
public static global::System.Delegate _auto_generated_Handler { get; } = Handle;
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("MinimalApiBuilder.Generator", "1.3.3.0")]
public static void _auto_generated_Configure(global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder builder)
{
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("MinimalApiBuilder.Generator", "1.3.3.0")]
public static void Configure(global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder builder)
{
}
}
Usefull
Download Example (.NET C# )
Share MinimalApiBuilder
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MinimalApiBuilder