Skip to content

maisiesadler/Endpoints

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Endpoints

NuGet version (Endpoints) Workflow status

Endpoints provides a more friendly way to set up functions for using AspNetCore Endpoint Routing.

Aims

  • Business logic shouldn't know anything about Http Request
  • Clear in tests what has been registered and what endpoints are being used
  • Few dependencies
  • Easy to extend
  • Simple

Endpoint routing example

services.AddTransient<IBusinessLogic>();
app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/endpoint", async httpContext => 
    {
        // parse model from httpContext.Request
        var model = httpContext.Request.RouteValues["id"]?.ToString();

        var result = await endpoints.ServiceProvider.GetRequiredService<IBusinessLogic>().Run(model);

        // set response in httpContext.Response
        httpContext.Response.StatusCode = (int)HttpStatusCode.OK;
        await httpContext.Response.WriteAsync(response.Name)
    });
})

Endpoints

Endpoints lets you define a Pipeline that defines how to parse the model and set the response. The business logic can then be added in separately.

services.AddTransient<IBusinessLogic>();
services.AddPipeline<Request, Response>(
    ModelParser.ParseModel,
    ModelParser.ParseResponse
);
app => app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/test", endpoints.ServiceProvider.Get<IBusinessLogic, Request, Response>(bl => bl.Run));
}));

Or if IBusinessLogic implements IRetriever<Request, Response>

app => app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/test", endpoints.ServiceProvider.Get<IBusinessLogic, Request, Response>());
}));

No input

If an endpoint has no input then there are overloads to create a pipeline with just a request.

services.AddTransient<IBusinessLogic>();
services.AddPipeline<Response>(
    ModelParser.ParseResponse
);

If IBusinessLogic implements IRetriever<Response>

app => app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/test", endpoints.ServiceProvider.Get<IBusinessLogic, Response>());
}));

Under the hood there is a type NoType that is used by the Pipeline<TIn, TOut> and then ignored by the retriever, so it should otherwise work as expected.

Getting started

Create new empty web project using dotnet new web.

Add Endpoints reference using dotnet add package Endpoints

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published