Files
Mccn/src/Common/Mccn.Common.Presentation/Endpoints/EndpointExtensions.cs
2026-03-15 11:22:01 +01:00

36 lines
1.2 KiB
C#

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;
}
}