init
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Mccn.Common.Presentation.Endpoints;
|
||||
|
||||
public static class EndpointExtensions
|
||||
{
|
||||
public static IServiceCollection AddEndpoints(
|
||||
this IServiceCollection services,
|
||||
params Assembly[] assemblies)
|
||||
{
|
||||
IEnumerable<Type> endpointTypes = assemblies
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(t => t is { IsAbstract: false, IsInterface: false } &&
|
||||
t.IsAssignableTo(typeof(IEndpoint)));
|
||||
|
||||
foreach (Type endpointType in endpointTypes) services.AddTransient(typeof(IEndpoint), endpointType);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder MapEndpoints(
|
||||
this WebApplication app,
|
||||
RouteGroupBuilder? routeGroupBuilder = null)
|
||||
{
|
||||
IEnumerable<IEndpoint> endpoints = app.Services.GetRequiredService<IEnumerable<IEndpoint>>();
|
||||
|
||||
IEndpointRouteBuilder builder = routeGroupBuilder is null ? app : routeGroupBuilder;
|
||||
|
||||
foreach (IEndpoint endpoint in endpoints) endpoint.MapEndpoint(builder);
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user