Skip to content
/ neat-api Public

A super simple library to help you modularize your minimal API projects.

License

Notifications You must be signed in to change notification settings

vantm/neat-api

Repository files navigation

NEAT API

NuGet Version GitHub last commit

A super simple library to help you modularize your minimal API projects.

Install

dotnet add package NeatApi

Usage

Add NeatApi to your app

Looking for detail implementations in the folder /samples. Here is the basic usage:

var builder = WebApplication.CreateBuilder(args);

+ builder.AddNeatApi();

var app = builder.Build();

+ app.MapNeatApi();

app.Run();

Register Services

public class WeatherForecastServices : IServiceModule
{
    public void Register(IServiceCollection services, IServiceModuleContext context)
    {
        services.AddSingleton<IWeatherForecastRepo, SampleWeatherForecastRepo>();

        services.AddOptionsWithValidateOnStart<WeatherForecastOptions>()
            .BindConfiguration(WeatherForecastOptions.Name);
    }
}

Define Routes

General Cases

public class WeatherForecastRoutes : IRoutingModule
{
    protected void AddRoutes(IEndpointRouteBuilder app, IRoutingModuleContext context)
    {
        app.MapGet("/weatherforecast", (IWeatherForecastRepo repo) =>
        {
            return repo.GetForecasts();
        })
        .WithName("GetWeatherForecast")
        .WithOpenApi();

        if (context.Environment.IsDevelopment())
        {
            app.MapGet("debug", () =>
            {
                return "dotnet v" + Environment.Version.ToString();
            });
        }
    }
}

Grouped Routes

public class WeatherForecastRoutes() : GroupedRoutingModuleBase("/weatherforecast")
{
    protected override void AddRoutes(IEndpointRouteBuilder app, IEndpointConventionBuilder convention, IRoutingModuleContext context)
    {
        convention.WithTags("WeatherForecast").WithOpenApi();

        app.MapGet("", (IWeatherForecastRepo repo) =>
        {
            return repo.GetForecasts();
        }).WithName("GetWeatherForecast");
    }
}

License

MIT

About

A super simple library to help you modularize your minimal API projects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages