https://github.com/deshansl/slices-endpoints-nuget
This NuGet package simplifies the implementation of minimal APIs within nested, non-static classes, making it an ideal choice for organizing endpoints in vertical slice architecture projects. It provides a clean, modular structure that enhances code organization by grouping related endpoints within feature-based classes.
https://github.com/deshansl/slices-endpoints-nuget
dotnet dotnetco endpoint minimal-api minimalapi nested software-architecture support vertical vertical-slice-architecture
Last synced: 4 months ago
JSON representation
This NuGet package simplifies the implementation of minimal APIs within nested, non-static classes, making it an ideal choice for organizing endpoints in vertical slice architecture projects. It provides a clean, modular structure that enhances code organization by grouping related endpoints within feature-based classes.
- Host: GitHub
- URL: https://github.com/deshansl/slices-endpoints-nuget
- Owner: DeshanSL
- Created: 2024-10-21T10:06:55.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-10-30T06:32:22.000Z (6 months ago)
- Last Synced: 2024-12-22T21:35:21.186Z (4 months ago)
- Topics: dotnet, dotnetco, endpoint, minimal-api, minimalapi, nested, software-architecture, support, vertical, vertical-slice-architecture
- Language: C#
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# slices-endpoints-nuget
This NuGet package simplifies the implementation of minimal APIs within nested, non-static classes, making it an ideal choice for organizing endpoints in vertical slice architecture projects. It provides a clean, modular structure that enhances code organization by grouping related endpoints within feature-based classes.## Start services
```bash
dotnet add package Slices.Endpoints.Mapper --version 1.0.0
```Current working assembly must be provided by referencing one of your classes
```csharp
app.UseSliceEndpoints(options =>
{
options.RegisterEndpointsFromAssembly(typeof(Program).Assembly);
});
```# Implementations
## Using IHasEndpoints interface
Being nested or implementing IFeature interface is not mandatory when using IHasEndpoints
```csharp
public class CreateOrder : IFeature
{
public record Command() : IRequest>
{
};internal sealed class Handler : IRequestHandler>
{
private readonly IAppEventStore _store;public Handler(IAppEventStore store)
{
_store = store;
}
public async Task> Handle(Command request, CancellationToken cancellationToken)
{
// Handler logic
}
}
// THIS ENDPOINT WILL BE MAPPED TO THE ROUTER BY THE PACKAGE
public class Endpoint : IHasEndpoints
{
public record Request()
{
}
public void Map(IEndpointRouteBuilder app)
{
app.MapPost("api/orders", async ([FromBody] Request request, [FromServices] IMediator mediator) =>
{
var result = await mediator.Send(new Command());
return result.Value;
});
}
}}
```## Using naming convention
MANDATORY to implement IFeature marker interface for declaring class/type of "Endpoints"
```csharp
// Implementing IFeature is MUST to use naming convention.
public class CreateOrder : IFeature
{
// Nested class should be named Endpoints
public class Endpoints
{
public record Request()
{
}
// Map method SHOULD accept IEndpointRouteBuilder
public void Map(IEndpointRouteBuilder app)
{
app.MapPost("api/orders", async ([FromBody] Request request, [FromServices] IMediator mediator) =>
{
var result = await mediator.Send(new Command());
return result.Value;
});
}
}
}```