Covali.Modules 9.0.0

dotnet add package Covali.Modules --version 9.0.0
                    
NuGet\Install-Package Covali.Modules -Version 9.0.0
                    
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="Covali.Modules" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Covali.Modules" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Covali.Modules" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Covali.Modules --version 9.0.0
                    
#r "nuget: Covali.Modules, 9.0.0"
                    
#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.
#:package Covali.Modules@9.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Covali.Modules&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Covali.Modules&version=9.0.0
                    
Install as a Cake Tool

Covali.Modules

NuGet License: MIT

Simplifies implementation of modular architecture in ASP.NET Core applications.

Features

  • Modular Architecture: Organize your application into self-contained, reusable modules
  • Automatic Discovery: Scan assemblies for module definitions automatically
  • Lifecycle Management: Separate phases for service registration and application configuration
  • Dependency Injection: Full integration with ASP.NET Core's built-in DI container
  • Module Registry: Track and manage all registered modules in your application
  • Hooks: Extensibility points for custom module processing

Installation

dotnet add package Covali.Modules

Quick Start

1. Define a Module

Create a module by implementing the IModuleDefinition interface:

using Covali.Modules;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

public class MyFeatureModule : IModuleDefinition
{
    public void AddModuleServices(
        IServiceCollection services,
        IConfiguration configuration,
        IHostEnvironment environment)
    {
        // Register services
        services.AddScoped<IMyService, MyService>();
        services.AddSingleton<IMyRepository, MyRepository>();

        // Add configuration
        services.Configure<MyFeatureOptions>(
            configuration.GetSection("MyFeature"));
    }

    public Task ConfigureModule(
        IApplicationBuilder app,
        IConfiguration configuration,
        ILogger logger,
        IHostEnvironment environment)
    {
        logger.LogInformation("MyFeature module configured");

        // Configure middleware, endpoints, etc.
        return Task.CompletedTask;
    }
}

2. Register Modules

There are several ways to register modules in your application:

Automatically discover and register all modules in specified assemblies:

var builder = WebApplication.CreateBuilder(args);

// Scan specific assemblies
builder.AddModulesServices(
    builder.Environment,
    typeof(MyFeatureModule).Assembly,
    typeof(AnotherModule).Assembly
);

var app = builder.Build();

// Configure all registered modules
await app.ConfigureModules();

app.Run();
Register by Type

Register modules from types:

builder.AddModulesServices(
    builder.Environment,
    typeof(MyFeatureModule),
    typeof(AnotherModule)
);
Manual Registration

Register specific modules explicitly:

builder.AddModuleServices<MyFeatureModule>(builder.Environment);

Core Components

IModuleDefinition

The main interface that all modules must implement:

public interface IModuleDefinition
{
    void AddModuleServices(
        IServiceCollection services,
        IConfiguration configuration,
        IHostEnvironment environment);

    Task ConfigureModule(
        IApplicationBuilder app,
        IConfiguration configuration,
        ILogger logger,
        IHostEnvironment environment);
}
  • AddModuleServices: Called during the service registration phase. Register all DI services here.
  • ConfigureModule: Called during the application configuration phase. Set up middleware, endpoints, etc.

ModuleRegistry

Access registered modules at runtime:

// Get all registered modules
var modules = ModuleRegistry.ModuleDefinitions;

// Get a specific module by type
var myModule = ModuleRegistry.Get<MyFeatureModule>();

// Get module by assembly name
var module = ModuleRegistry.GetByAssemblyName("MyFeature");

ModuleHook

Extensibility point for custom module processing:

public static class ModuleHook
{
    public static readonly Action<IServiceCollection, IModuleDefinition>? ModuleServicesConfigured;
}

Extension Methods

Service Registration

  • AddModuleServices<TModule>() - Register a specific module
  • AddModulesServices(params Assembly[]) - Auto-discover and register modules from assemblies
  • AddModulesServices(params Type[]) - Register modules from types

Application Configuration

  • ConfigureModules() - Configure all registered modules
  • ConfigureModule<TModule>() - Configure a specific module
  • ConfigureModule(IModuleDefinition) - Configure a module instance

Usage Examples

Example: Feature-Based Module

public class UserManagementModule : IModuleDefinition
{
    public void AddModuleServices(
        IServiceCollection services,
        IConfiguration configuration,
        IHostEnvironment environment)
    {
        services.AddScoped<IUserService, UserService>();
        services.AddScoped<IUserRepository, UserRepository>();

        if (environment.IsDevelopment())
        {
            services.AddScoped<IUserSeeder, DevelopmentUserSeeder>();
        }
    }

    public async Task ConfigureModule(
        IApplicationBuilder app,
        IConfiguration configuration,
        ILogger logger,
        IHostEnvironment environment)
    {
        logger.LogInformation("User Management module initialized");

        if (environment.IsDevelopment())
        {
            using var scope = app.ApplicationServices.CreateScope();
            var seeder = scope.ServiceProvider.GetRequiredService<IUserSeeder>();
            await seeder.SeedAsync();
        }
    }
}

Example: Infrastructure Module

public class DatabaseModule : IModuleDefinition
{
    public void AddModuleServices(
        IServiceCollection services,
        IConfiguration configuration,
        IHostEnvironment environment)
    {
        var connectionString = configuration.GetConnectionString("Default");

        services.AddDbContext<AppDbContext>(options =>
            options.UseNpgsql(connectionString));

        services.AddScoped<IUnitOfWork, UnitOfWork>();
    }

    public async Task ConfigureModule(
        IApplicationBuilder app,
        IConfiguration configuration,
        ILogger logger,
        IHostEnvironment environment)
    {
        using var scope = app.ApplicationServices.CreateScope();
        var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();

        await dbContext.Database.MigrateAsync();
        logger.LogInformation("Database migrations applied");
    }
}

Best Practices

  1. Single Responsibility: Each module should represent a cohesive set of functionality
  2. Independence: Modules should be as independent as possible
  3. Configuration: Use the IConfiguration parameter to make modules configurable
  4. Environment-Aware: Leverage IHostEnvironment for environment-specific behavior
  5. Logging: Use the provided ILogger to log module initialization
  6. Async Operations: Use the async ConfigureModule method for any async initialization

Requirements

  • .NET 9.0 or higher
  • ASP.NET Core

License

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

Author

cva_pasha

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
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
9.0.0 223 11/9/2025