EndpointProviders 1.0.1
See the version list below for details.
dotnet add package EndpointProviders --version 1.0.1
NuGet\Install-Package EndpointProviders -Version 1.0.1
<PackageReference Include="EndpointProviders" Version="1.0.1" />
paket add EndpointProviders --version 1.0.1
#r "nuget: EndpointProviders, 1.0.1"
// Install EndpointProviders as a Cake Addin #addin nuget:?package=EndpointProviders&version=1.0.1 // Install EndpointProviders as a Cake Tool #tool nuget:?package=EndpointProviders&version=1.0.1
EndpointProviders
The simplest way to add endpoints dynamically for Minimal API empowered by Dependency Injection principles.
Why another one?
Most of the libraries that target Minimal API functionality deal with instances of classes that can add Endpoints dynamically via a "marker" interface. What is the approach then and why this is not fully practical? Let's see an example below.
public class SampleWithEndPoints : IMarker
{
public void AddEndpoints (WebApplication app)
{
app.MapGet("/api/...", Handler1);
app.MapGet("/api/...", Handler2);
app.MapGet("/api/...", Handler3);
app.MapPost("/api/...", Handler4);
...
}
//c'mon it's not practical to add Dependency Injection for each method
//wouldn't it be nice if we added the IRepository repo in the constructor of the class and make all these handlers not static?
public static IResult Handler1(IRepository repo, int id) => {...}
public static IResult Handler2(IRepository repo, int id) => {...}
public static IResult Handler3(IRepository repo, int id) => {...}
public static IResult Handler4(IRepository repo, int id) => {...}
}
...
//and at some point register all endpoints via a method which scans all the IMarker classes in the assembly/app.
app.RegisterEndPoints(typeof(App));
What annoys me is that the role of the IMarker
interface in the example above, is used only for automatic registration of all endpoints.
The most practical way would be to ALSO use the IMarker
to allow Dependency Injection within the SampleWithEndPoints
without having to write EVERY time for EACH handler the IRepository repo
.
The solution: EndpointProviders!
How to install
Via tha Package Manager:
Install-Package EndpointsProviders
Via the .NET CLI
dotnet add package EndpointsProviders
How to use
For this library, each class the instance of which we want to add endpoints, should derive the EndpointProvider
abstract class. The EndpointProvider
inherits the IEndpointProvider
interface.
Each class that derives from EndpointProvider
should override the AddEndpoints
method
The above example should now be written as:
public class SampleWithEndPoints : EndpointProvider
{
readonly IRepository _repo;
//Each EndpointProvider should have exactly the constructor below. The provider can then be used to create other instances via Dependency Injection in the constructor.
public SampleWithEndPoints(IServiceProvider provider) : base(provider)
{
repo = provider.GetService<IRepository>();
}
public void AddEndpoints (WebApplication app)
{
app.MapGet("/api/...", Handler1);
app.MapGet("/api/...", Handler2);
app.MapGet("/api/...", Handler3);
...
}
//we can now use every instance that we created, without having to inject for each handler the IRepository!
//and the handlers are not static now!
public IResult Handler1(int id) => { ... _repo.DoSth(id); ...}
public IResult Handler2(int id) => {...}
public IResult Handler3(int id) => {...}
}
To add all endpoints from classes with the IEndpointProvider
interface, we should use 2 methods as shown below:
using EndpointProviders;
...
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.AddEndpointProviderFactory(); //1st method: call before building the app
...
WebApplication app = builder.Build();
//this adds the endpoints - the application must be first build
//the method gets as inputs any class that can identify its parent Assembly
//in the example below, the MarkerClass identifies the assembly
app.AddEndpointsFromEndpointProviders(typeof(MarkerClass)); //2nd method: call after building the app. That's it!
The AndEndpointProviderFactory
method is reponsible for the services registration and passing each IServiceProvider
to each EndpointProvider
class constructor.
The final method AddEndpointsFromEndpointProviders
is the method that finally adds the endpoints to the current Web API app.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.