An open API service indexing awesome lists of open source software.

https://github.com/lucasmontagnani/idempotentapi

This project explores different ways to handle API idempotency. It implements service injection, action filters, and middleware to enforce idempotency in POST requests, using an In-Memory database for testing.
https://github.com/lucasmontagnani/idempotentapi

api csharp idempotency netcore rest-api

Last synced: 8 months ago
JSON representation

This project explores different ways to handle API idempotency. It implements service injection, action filters, and middleware to enforce idempotency in POST requests, using an In-Memory database for testing.

Awesome Lists containing this project

README

          

# IdempotentApi

## Overview

This project, IdempotencyAPI, was created to explore different ways to handle API idempotency. Existing NuGet packages caused conflicts with parts of the code, leading to errors, so I implemented custom solutions.

To demonstrate these solutions, the project includes several endpoints that validate idempotency in different ways. An in-memory database is used with a single Product model to test the results.

## Endpoints
### Product Retrieval


  • GET /GetProductsById - Retrieves a product by its ID.

  • GET /GetProducts - Retrieves all products currently stored in the database.

### Product Creation


  • POST /CreateProduct - A standard product creation endpoint (control case) without idempotency validation.

### Idempotent Product Creation

Each of the following endpoints requires a header Idempotency-Key, which must be a valid GUID.


  • POST /CreateProductIdempotentWithServiceInjection

    • Uses an IdempotencyService, injected via Dependency Injection in the controller, to validate the request.

  • POST /CreateProductIdempotentWithFilter

    • Uses an Action Filter triggered by the [ServiceFilter] attribute on the endpoint to validate idempotency.

  • POST /CreateProductIdempotentWithMiddleware

    • Uses Middleware to validate idempotency. This is applied globally to all POST requests that include the Idempotency-Key header.

## Comparison of Idempotency Approaches


Approach
Pros
Cons


Service Injection
Simple to implement
Increases code dependency and coupling


Action Filter
Loose coupling, applies idempotency selectively
Slightly more setup required


Middleware
Loose coupling, automatically applies to all POSTs
Applies idempotency to all POST endpoints

## Conclusion

Among the three approaches, Action Filters offer the best balance between flexibility and maintainability, allowing idempotency to be applied selectively while keeping the code loosely coupled.