Infinity Toolkit is a collection of useful utilities simplifying development of modular monoliths and applications using vertical slice architecture.
- Feature modules - Let's you automatically register dependencies and endpoints in modules which simplifies development when you are working in feature slices.
- Mediator - A bare bones mediator test. (WIP)
- Logging formatter - logging formatter with a Visual Studio Code inspired theme and Serilog like formatting
Infinity.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:
- Look at the sample project in the repository. The sample project found here FeatureModulesSample is a simple web api with two feature modules.
- Create a new project and integrate the library.
Let's look create a new project and integrate the library.
- Create a new web api project using the dotnet cli.
dotnet new webapi -n MyWebApi
- Add the Infinity.Toolkit.FeatureModules package to the project.
dotnet add package Infinity.Toolkit
- In Program.cs, add the following code:
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.
- Run the application and make sure that it is working. The application should return a 404 error since there are no endpoints mapped to the application.
- Create a new class called WeatherModule.cs in the root of the project. Make sure to remove the endpoint that was created by the template. Add the following code to the WeatherModule.cs file.
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);
}
- Run the application and navigate to the /weatherforecast endpoint. You should see the weather forecast.
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:
- FeatureModule
- WebFeatureModule
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
.
A simple mediator implementation that can be used to send messages between different parts of the application. This is a work in progress and will be updated with more features and examples. Currently this is a test using TPL Dataflow.
A logging formatter that formats log messages with a Visual Studio Code inspired theme and Serilog like formatting.
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.