ServiceCollection.Extensions.Modules 3.0.1

dotnet add package ServiceCollection.Extensions.Modules --version 3.0.1                
NuGet\Install-Package ServiceCollection.Extensions.Modules -Version 3.0.1                
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="ServiceCollection.Extensions.Modules" Version="3.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ServiceCollection.Extensions.Modules --version 3.0.1                
#r "nuget: ServiceCollection.Extensions.Modules, 3.0.1"                
#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 ServiceCollection.Extensions.Modules as a Cake Addin
#addin nuget:?package=ServiceCollection.Extensions.Modules&version=3.0.1

// Install ServiceCollection.Extensions.Modules as a Cake Tool
#tool nuget:?package=ServiceCollection.Extensions.Modules&version=3.0.1                

N|Solid

ServiceCollection.Extensions.Modules

We all love to have modules to simplify registrations on our DI framework of choice.

Sadly Microsoft.Extensions.DependencyInjection doesn't have builtin modules.

Here is a naive, simple implementation that has been working for me.

You cannot register twice the same module in the same IServiceCollection, but you can register the same Module across different Collections.

How to use it

  1. Inherit from ServiceCollection.Extensions.Modules.Module class
namespace MyFanceModulesTest
{
    using Microsoft.Extensions.DependencyInjection;
    using ServiceCollection.Extensions.Modules;

    public class MyFancyModule : Module
    {
        protected override void Load(IServiceCollection services)
        {
            base.Load(services);
            services.AddSingleton<TSERVICE, TSERVICEIMPL>();            
        }
    }
}
  1. Register your module
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.RegisterModule<MyFancyModule>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // More stuff here...
    }
}
  • Boom! modules, modules, modules!!

Nested Modules

You can register modules inside modules....

namespace MyFancyNestedModulesTest
{
    using Microsoft.Extensions.DependencyInjection;
    using ServiceCollection.Extensions.Modules;

    public class InnerModule : Module
    {
        protected override void Load(IServiceCollection services)
        {
            base.Load(services); 
            // SOME FANCY REGISTRATIONS HERE
        }
    }

    public class OuterModule : Module
    {
        protected override void Load(IServiceCollection services)
        {
            base.Load(services);
            services.RegisterModule<InnerModule>();
            // MORE FANCY REGISTRATIONS HERE
         }
    }
}

Parameterized Modules

You can register instances of modules if you need some parameter....

namespace MyParameterizedModulesTest
{
    using Microsoft.Extensions.DependencyInjection;
    using ServiceCollection.Extensions.Modules;

    public class ParameterizedModule : Module
    {
        private readonly string _url;

        public ParameterizedModule(string url)
        {
        	_url = _url;
        }
		
        protected override void Load(IServiceCollection services)
        {
            base.Load(services); 
            // SOME FANCY REGISTRATIONS HERE using _url
        }
    }

    // and later on
    services.RegisterModule(new ParameterizedModule("https://google.com"));
}

Modules registration via Action

An action can be provided to register modules....

namespace MyParameterizedModulesTest
{
    using Microsoft.Extensions.DependencyInjection;
    using ServiceCollection.Extensions.Modules;

    public class ParameterizedModule : Module
    {
        private readonly string _url;

        public ParameterizedModule(string url)
        {
        	_url = _url;
        }
		
        protected override void Load(IServiceCollection services)
        {
            base.Load(services); 
            // SOME FANCY REGISTRATIONS HERE using _url
        }
    }

    // and later on
    services
        .RegisterModule(
		(c) =>
	        {
                    var configuration = c.BuildServiceProvider().GetRequiredService<IConfiguration>();
                    return new ParameterizedModule(configuration["ApiUrl"]);
	        });
}

Logo Provided by Vecteezy

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
3.0.1 6,931 2/12/2024
3.0.0 4,594 10/30/2023
2.3.1 122,614 11/9/2021
2.3.0 19,512 12/28/2020
2.2.0 633 11/12/2020
2.2.0-rc.2.20475.5 233 11/1/2020
2.1.0 402 11/6/2020
2.0.0 7,777 6/9/2020
1.0.0 578 4/5/2020

Fix single loading