Giter VIP home page Giter VIP logo

minimal-api-slim-endpoints's Introduction

MinimalApi Slim Endpoints

Github actions Nuget feed

Small library for decoupling endpoints in MinimalApi from the Program.cs file.

This library does not add any additional overhead to the application, it just helps you to organize your code better.

The source generator will generate a DependencyInjection extension that will contain all the endpoints that you have defined in your application.

Installation

Install the package from NuGet:

dotnet add package MinimalApi.SlimEndpoints

Quick start

Create a class that inherits from ISlimEndpoint and implement the Configure and Handler methods. Add the SlimEndpoint attribute to the class.

SlimEndpoint attribute

The SlimEndpoint attribute has the following properties:

  • Path - The path of the endpoint. This is the same as the MapGet method in the MinimalApi.
  • HttpMethod - The HTTP method of the endpoint. This is the same as the MapGet method in the MinimalApi.

Configure method

The Configure method injects the RouteHandlerBuilder into the endpoint class. Leave the method empty if you don't need to configure the endpoint.

Handler method

The Handler method is the main method of the endpoint. It is called when the endpoint is hit.

Example

using MinimalApi.SlimEndpoints.Abstractions;

namespace SampleDemo.Endpoints;

[SlimEndpoint(Path = "/hello", Method = "GET")]
public partial class HelloEndpoint : ISlimEndpoint
{
    public void Configure(RouteHandlerBuilder builder)
    {
        builder.AllowAnonymous();
    }

    public Delegate Handler => (() => "Hello World!");
}

Dependency Injection configuration

The MinimalApi.SlimEndpoints library uses the Microsoft.Extensions.DependencyInjection library for dependency injection. You can configure the DI container in the Program.cs file.

var builder = WebApplication.CreateBuilder(args);

// Register endpoints
builder.Services.AddSlimEndpoints();

var app = builder.Build();

// Map endpoints
app.MapSlimEndpoints();

app.Run();

Common pitfalls

Please do not capture any services in the Handler method that are injected into the endpoint class. Instead, inject the services into the Handler method directly.

Injected services into the endpoint class should only be used in the Configure method.

โŒ BAD This example uses a service that is injected into the endpoint class.

[SlimEndpoint(Path = "/hello", Method = "GET")]
public partial class HelloEndpoint : ISlimEndpoint
{
    private readonly ExampleService _service;
    
    public HelloEndpoint(ExampleService service)
    {
        _service = service;
    }

    public void Configure(RouteHandlerBuilder builder)
    {
        builder.AllowAnonymous();
    }

    public Delegate Handler => (() => _service.GetExample());
}

โœ… GOOD This example injects the service directly into the Handler method.

[SlimEndpoint(Path = "/hello", Method = "GET")]
public partial class HelloEndpoint : ISlimEndpoint
{
    public void Configure(RouteHandlerBuilder builder)
    {
        builder.AllowAnonymous();
    }

    public Delegate Handler => ((ExampleService service) => service.GetExample());
}

Note: Please see the SampleDemo project for a complete example.

Change log

Please see the CHANGELOG for more information on what has changed recently.

License

MIT

minimal-api-slim-endpoints's People

Stargazers

 avatar

Watchers

 avatar

Forkers

wuuer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.