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.
- Host: GitHub
- URL: https://github.com/lucasmontagnani/idempotentapi
- Owner: lucasMontagnani
- Created: 2025-01-29T04:18:21.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-30T18:56:18.000Z (over 1 year ago)
- Last Synced: 2025-06-05T10:49:26.532Z (about 1 year ago)
- Topics: api, csharp, idempotency, netcore, rest-api
- Language: C#
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.