π The Problem: Repetitive API Code
Every .NET developer writes this boilerplate:
// BEFORE: 15+ lines for ONE endpoint
app.MapGet("/api/products/{id}", async (int id, ProductService service) =>
{
if (id <= 0) return Results.BadRequest("Invalid ID");
var product = await service.GetProductAsync(id);
return product == null
? Results.NotFound("Product not found")
: Results.Ok(product);
});
π The Solution: Source Generator Magic
π¦ Setup:
// Program.cs - One time setup
using REslava.Result.SourceGenerators;
[assembly: GenerateResultExtensions()]
β¨ The Magic:
// AFTER: 3 lines total!
app.MapGet("/api/products/{id}", async (int id, ProductService service) =>
=> (await service.GetProductAsync(id)).ToIResult());
That's it! 80% code reduction! π€―
π Smart Error Mapping
The generator automatically handles errors:
"not found" β 404
"validation" β 422
"unauthorized" β 401
"forbidden" β 403
"conflict" β 409
π Real Impact
Operation Before After Reduction
GET 15 lines 3 lines 80%
POST 20 lines 3 lines 85%
PUT 18 lines 3 lines 83%
π― Complete API Example
π BEFORE (50+ lines):
app.MapGet("/api/products", async (ProductService service) =>
{
try { return Results.Ok(await service.GetAllProductsAsync()); }
catch { return Results.Problem("Error", statusCode: 500); }
});
app.MapGet("/api/products/{id}", async (int id, ProductService service) =>
{
if (id <= 0) return Results.BadRequest("Invalid ID");
var product = await service.GetProductAsync(id);
return product == null ? Results.NotFound() : Results.Ok(product);
});
app.MapPost("/api/products", async (CreateProductRequest request, ProductService service) =>
{
if (string.IsNullOrWhiteSpace(request.Name))
return Results.ValidationProblem(new { Name = new[] { "Required" } });
var product = await service.CreateProductAsync(request);
return Results.Created($"/api/products/{product.Id}", product);
});
π AFTER (9 lines):
app.MapGet("/api/products", async (ProductService service) =>
=> (await service.GetAllProductsAsync()).ToIResult());
app.MapGet("/api/products/{id}", async (int id, ProductService service) =>
=> (await service.GetProductAsync(id)).ToIResult());
app.MapPost("/api/products", async (CreateProductRequest request, ProductService service) =>
=> (await service.CreateProductAsync(request)).ToPostResult(p => $"/api/products/{p.Id}"));
π― HTTP Method Extensions
.ToIResult() // GET - 200/400/404/500
.ToPostResult() // POST - 201 Created
.ToPutResult() // PUT - 200 OK
.ToDeleteResult() // DELETE - 200 OK
π Get Started
π¦ Install:
dotnet add package REslava.Result
dotnet add package REslava.Result.SourceGenerators
β¨ Setup:
using REslava.Result.SourceGenerators;
[assembly: GenerateResultExtensions()]
π― Use:
return await service.GetDataAsync().ToIResult();
π Benefits
π 80-90% Code Reduction
π§ Smart Error Handling
π RFC 7807 Compliant
β‘ Zero Runtime Overhead
π§ Compile-Time Generation
π Conclusion
Source generators transform .NET API development from repetitive boilerplate to elegant, declarative code.
Try it today and never write manual HTTP responses again! π
π Learn More
π¦ NuGet Package
π Documentation
π Live Samples
Top comments (0)