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
<PackageReference Include="ServiceCollection.Extensions.Modules" Version="3.0.1" />
paket add ServiceCollection.Extensions.Modules --version 3.0.1
#r "nuget: ServiceCollection.Extensions.Modules, 3.0.1"
// 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
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
- 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>();
}
}
}
- 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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 2.0.0 && < 3.0.0)
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection (>= 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fix single loading