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 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 endpoints = app.Services.GetRequiredService>(); IEndpointRouteBuilder builder = routeGroupBuilder is null ? app : routeGroupBuilder; foreach (IEndpoint endpoint in endpoints) endpoint.MapEndpoint(builder); return app; } }