-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Code(Cross-cutting concerns): Add abstraction to handle endpoints for…
… minimal API
1 parent
b52005e
commit e07f837
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Shared/StellarChat.Shared.Abstractions/API/Endpoints/IEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace StellarChat.Shared.Abstractions.API.Endpoints; | ||
|
||
public interface IEndpoint | ||
{ | ||
void Register(IServiceCollection services, IConfiguration configuration); | ||
void Use(IApplicationBuilder app); | ||
void Expose(IEndpointRouteBuilder endpoints); | ||
|
||
protected static IResult Select<TData>(TData data) | ||
where TData : class | ||
=> data is null | ||
? Results.NotFound() | ||
: Results.Ok(data); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Shared/StellarChat.Shared.Infrastructure/API/Endpoints/Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using StellarChat.Shared.Abstractions.API.Endpoints; | ||
|
||
namespace StellarChat.Shared.Infrastructure.API.Endpoints; | ||
|
||
public static class Extensions | ||
{ | ||
private static readonly List<IEndpoint> registeredEndpoints = new(); | ||
|
||
public static IServiceCollection RegisterEndpoints(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
var endpoints = DiscoverEndpoints(); | ||
|
||
foreach (var endpoint in endpoints) | ||
{ | ||
endpoint.Register(services, configuration); | ||
registeredEndpoints.Add(endpoint); | ||
} | ||
|
||
return services; | ||
} | ||
|
||
public static IApplicationBuilder UseEndpoints(this IApplicationBuilder app) | ||
{ | ||
foreach (var endpoint in registeredEndpoints) | ||
{ | ||
endpoint.Use(app); | ||
} | ||
|
||
return app; | ||
} | ||
|
||
public static WebApplication MapEndpoints(this WebApplication app) | ||
{ | ||
foreach (var endpoint in registeredEndpoints) | ||
{ | ||
endpoint.Expose(app); | ||
} | ||
|
||
return app; | ||
} | ||
|
||
private static IList<IEndpoint> DiscoverEndpoints() | ||
=> AppDomain.CurrentDomain | ||
.GetAssemblies() | ||
.SelectMany(x => x.GetTypes()) | ||
.Where(p => p.IsClass && p.IsAssignableTo(typeof(IEndpoint))) | ||
.Select(Activator.CreateInstance) | ||
.Cast<IEndpoint>() | ||
.ToList(); | ||
} |