Skip to content

CoderFoundry/minimalAPIPatterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minimal API Patterns

A demonstration of two patterns for structuring ASP.NET Core Minimal APIs using extension methods. These are examples of common project structures with the goal of demonstrating how to define minimal API endpoints without cluttering Program.cs.

  • Controller-style pattern: Defines endpoints in a way similar to MVC controllers by grouping related routes and handlers.
  • REPR pattern: Follows the Request, Endpoint, Response structure for each resource, without external libraries.

This repository showcases three extension methods:

  1. MapCustomerEndpoints – MVC-like grouping for Customer endpoints.
  2. MapOrderEndpoints – REPR pattern for Order endpoints.
  3. MapProductEndpoints – REPR pattern for Product endpoints.

📺 Video Walkthrough

Watch the full walkthrough on YouTube:

Minimal API Patterns Walkthrough

🚀 Getting Started

Prerequisites

Running the Examples

  1. Clone the repository:

    git clone https://github.com/CoderFoundry/minimalAPIPatterns.git
    cd minimalAPIPatterns
  2. Run the project:

    cd minimalapistructure
    dotnet run 
  3. View the API Documentation by visiting https://localhost:7127/ in your browser

🏗️ Patterns

Customer Endpoints (Controller Pattern)

Defined in CustomerEndpoints.cs using the MapCustomerEndpoints extension method:

public static class CustomerEndpoints
{
    public static IEndpointRouteBuilder MapCustomerEndpoints(this IEndpointRouteBuilder route)
    {
        var group = route.MapGroup("/customers").WithTags("Customers");

        group.MapGet("", GetCustomers);
        group.MapGet("{id:int}", GetCustomerById);
        group.MapPost("", CreateCustomer);
        group.MapPut("{id:int}", UpdateCustomer);
        group.MapDelete("{id:int}", DeleteCustomer);

        return group;
   }

    // Handler methods (GetCustomers, GetCustomerById, etc.) live in the same file.
    private static async Task<Ok<IEnumerable<CustomerResponse>>> GetCustomers(ICustomerService svc)
    {
        return TypedResults.Ok(
            await svc.GetCustomersAsync()
        );
    }
    // ...other methods below...
}

Order Endpoints (REPR Pattern)

Defined in OrderEndpoints.cs using the MapOrderEndpoints extension method:

public static class OrderEndpoints
{
   public static IEndpointRouteBuilder MapOrderEndpoints(this IEndpointRouteBuilder route)
   {
       var group = route.MapGroup("/orders").WithTags("Orders");

       group.MapGet("", GetOrdersHandler.Handle);
       group.MapGet("{id:int}", GetOrderByIdHandler.Handle);
       group.MapPost("", CreateOrderHandler.Handle);
       group.MapPut("{id:int}", UpdateOrderHandler.Handle);
       group.MapDelete("{id:int}", DeleteOrderHandler.Handle);
      
       return group;
   }

   // Each endpoint has its own folder with its handler, request DTO, and response DTO
}

Each endpoint in the REPR pattern has its own folder for the endpoint, located in Endpoints/Orders.

About

minimal API Patterns

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages