Infinity.Toolkit.FeatureModules 1.0.2

dotnet add package Infinity.Toolkit.FeatureModules --version 1.0.2                
NuGet\Install-Package Infinity.Toolkit.FeatureModules -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Infinity.Toolkit.FeatureModules" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Infinity.Toolkit.FeatureModules --version 1.0.2                
#r "nuget: Infinity.Toolkit.FeatureModules, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Infinity.Toolkit.FeatureModules as a Cake Addin
#addin nuget:?package=Infinity.Toolkit.FeatureModules&version=1.0.2

// Install Infinity.Toolkit.FeatureModules as a Cake Tool
#tool nuget:?package=Infinity.Toolkit.FeatureModules&version=1.0.2                

Infinity.Toolkit.FeatureModules

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.

Quick Start

To get started with Feature Modules there are two options:

  1. Look at the sample project in the repository. The sample project found here FeatureModulesSample is a simple web api with two feature modules.
  2. Create a new project and integrate the library.

Let's look create a new project and integrate the library.

  1. Create a new web api project using the dotnet cli.
dotnet new webapi -n MyWebApi
  1. Add the Infinity.Toolkit.FeatureModules package to the project.
dotnet add package Infinity.Toolkit
  1. 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.

  1. 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 feature module

  1. 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);
}
  1. Run the application and navigate to the /weatherforecast endpoint. You should see the weather forecast.

Feature modules in a class library

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.

Types of feature modules

There are two types of feature modules:

  1. FeatureModule
  2. 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.

Contributing

If you have any ideas, suggestions or issues, please create an issue or a pull request. Or reach out to me on BlueSky.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 93 11/15/2024