Infinity Toolkit Feature Modules let's you automatically register dependencies and endpoints in modules which simplifies development when you are working with vertical feature slices.
$ dotnet add package Infinity.Toolkit.FeatureModulesInfinity.Toolkit.FeatureModules is a library that simplifies development applications where you want to split functionality in different modules. It is especially useful when you are working with vertical slices in a modular monolith or application. However though, the library can be used in any type of application. It let's you automatically register dependencies and endpoints in modules which simplifies development when you are working in feature slices.
To get started with Feature Modules there are two options:
Let's look create a new project and integrate the library.
dotnet new webapi -n MyWebApi
dotnet add package Infinity.Toolkit
using Infinity.Toolkit.FeatureModules;
var builder = WebApplication.CreateBuilder(args);
builder.AddFeatureModules();
var app = builder.Build();
app.MapFeatureModules();
app.Run();
This will add all feature modules added to the project and add the modules services to the application. The MapFeatureModules method will map all endpoints to the application.
internal class WeatherModule : WebFeatureModule
{
public override IModuleInfo? ModuleInfo { get; } = new FeatureModuleInfo("WeatherModule", "1.0.0");
public override void MapEndpoints(IEndpointRouteBuilder builder)
{
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
builder.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
)).ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
}
}
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
The sample project also refers to a class library with another feature module. When you add the class library to the project, the feature module will be automatically registered and the endpoints will be mapped to the application.
There are two types of feature modules:
The difference is that the WebFeatureModule has access to the IEndpointRouteBuilder which allows you to map endpoints to the application.
To create a web feature module, you need to create a class that inherits from WebFeatureModule or implements IWebFeatureModule.
If you have any ideas, suggestions or issues, please create an issue or a pull request. Or reach out to me on BlueSky.
This project is licensed under the MIT License - see the LICENSE file for details.