Svrooij.EndpointMapper
0.3.2
dotnet add package Svrooij.EndpointMapper --version 0.3.2
NuGet\Install-Package Svrooij.EndpointMapper -Version 0.3.2
<PackageReference Include="Svrooij.EndpointMapper" Version="0.3.2" />
<PackageVersion Include="Svrooij.EndpointMapper" Version="0.3.2" />
<PackageReference Include="Svrooij.EndpointMapper" />
paket add Svrooij.EndpointMapper --version 0.3.2
#r "nuget: Svrooij.EndpointMapper, 0.3.2"
#:package Svrooij.EndpointMapper@0.3.2
#addin nuget:?package=Svrooij.EndpointMapper&version=0.3.2
#tool nuget:?package=Svrooij.EndpointMapper&version=0.3.2
π Svrooij.EndpointMapper
Stop writing boilerplate. Start building APIs. β‘
A powerful .NET source generator that eliminates repetitive code when building ASP.NET Core minimal APIs. Write less, ship more, and enjoy not having to write boilerplate code.
Currently this project is an experimental prototype. Features might be added or changed in future releases.
Why You'll Love It
β¨ Zero Runtime Overhead β All magic happens at compile time, not runtime
π― Type-Safe β Full IntelliSense support with generated code
π¦ Less Boilerplate β Auto-discover and register endpoints effortlessly
π Smart DTO Mapping β Only select what you need from your database
β
FluentValidation Integration β Validation filters generated automatically
π οΈ Easy to Use β Minimal setup, maximum productivity
Get Started in Seconds
Install the NuGet package:
dotnet add package Svrooij.EndpointMapper
Done! No configuration needed. The source generator does the rest. β¨
<details> <summary>Or add it manually to your project file</summary>
<ItemGroup>
<PackageReference Include="Svrooij.EndpointMapper" Version="1.0.0"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</details>
Features
Being a source generator, this library eliminates boilerplate by generating code at compile time. No reflection, no runtime discoveryβjust blazing-fast, type-safe endpoint management.
π― Easy Endpoint Mapping
Never write repetitive endpoint registration again. Simply implement IMapEndpoint and watch it get automatically discovered and registered.
public class WeatherEndpoint : IMapEndpoint
{
public void MapEndpoint(IEndpointRouteBuilder app)
{
app.MapGet("/weather", () => "Sunny")
.WithName("GetWeather")
.WithOpenApi();
app.MapPost("/weather", (WeatherRequest request) =>
Results.Ok($"Weather updated to {request.Description}"))
.WithName("UpdateWeather")
.WithOpenApi();
}
}
// In Program.cs - that's it!
app.MapEndpointFromMyApi(); // β¨ Auto-generated extension method
What you get:
- π Automatic discovery of all
IMapEndpointimplementations - ποΈ Compile-time code generation (zero runtime cost)
- π Organized, maintainable code structure
- π¦ Project-specific extension methods
π‘ Selective DTO Mapping
Stop fetching entire database records just to return a few fields. Generate optimized property selectors at compile timeβno reflection, no LINQ expressions. Original idea.
// Your domain model
public class User
{
public int Id { get; set; }
public required string Name { get; set; }
public required string Email { get; set; }
public string? Address { get; set; }
}
// Your DTO with automatic mapping generation
[Svrooij.EndpointMapper.GenerateSelect(typeof(User))]
public class UserDto
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Email { get; set; }
public string? Address { get; set; }
}
The magic:
- β¨ Generates two extension methods automatically
- π
SelectUserDto(IQueryable<User>, properties)β for Entity Framework queries - π
SelectUserDto(User, properties)β for object mapping - π Pure, optimized code generation (no reflection at runtime)
- ποΈ Select
nullfor not selected columns, before querying the database.
Real-world example:
// Database query
var userDtos = await dbContext.Users
.SelectUserDto("Id,Name") // π― Only fetch these columns!
.ToListAsync();
Perfect for APIs where clients can request specific fields!
This feature shines when you have large entities with many properties but your API consumers only need a few. Save bandwidth, reduce N+1 queries, and let the compiler do the heavy lifting.
β FluentValidation Endpoint Filter
Validation code tends to be verbose and repetitive. Not anymore. Automatically generate endpoint filters for all your AbstractValidator<T> implementations.
public class FluentValidationEndpoint : Svrooij.EndpointMapper.IMapEndpoint
{
public void MapEndpoint(IEndpointRouteBuilder app)
{
app.MapPost("/users", PostNewUserAsync)
.WithOpenApi()
.WithTags("Users")
.AddValidationWithUserDtoValidator(); // β¨ Auto-generated!
}
private static async Task<IResult> PostNewUserAsync(UserDto userDto)
=> Results.Ok($"User {userDto.Name} created!");
}
// Your validator
public class UserDtoValidator : AbstractValidator<UserDto>
{
public UserDtoValidator()
{
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Name is required")
.MinimumLength(2).WithMessage("At least 2 characters");
}
}
What's generated:
- π‘οΈ
UserDtoValidatorFilter : IEndpointFilter - π§
AddValidationWithUserDtoValidator()extension method
See the complete example for more details.
Examples & Samples
Want to see it in action? Check out the complete sample project with:
- π Multiple endpoint examples
- π Selective DTO mapping usage
- β FluentValidation integration
- π§ͺ Full test suite
Roadmap & Contributing
This is an active open-source project! Have ideas? Found a bug? Contributions are welcome!
Feel free to:
- π Report issues
- π‘ Suggest features
- π§ Submit pull requests
License
MIT License β see the LICENSE file for details.
Source Generators inspiration
- Andrew Lock Source Generators series
- Microsoft Docs on Source Generators
- PowerShell Source generator
Happy building! π
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.