Futurum.Microsoft.Extensions.DependencyInjection
2.0.1
dotnet add package Futurum.Microsoft.Extensions.DependencyInjection --version 2.0.1
NuGet\Install-Package Futurum.Microsoft.Extensions.DependencyInjection -Version 2.0.1
<PackageReference Include="Futurum.Microsoft.Extensions.DependencyInjection" Version="2.0.1" />
paket add Futurum.Microsoft.Extensions.DependencyInjection --version 2.0.1
#r "nuget: Futurum.Microsoft.Extensions.DependencyInjection, 2.0.1"
// Install Futurum.Microsoft.Extensions.DependencyInjection as a Cake Addin #addin nuget:?package=Futurum.Microsoft.Extensions.DependencyInjection&version=2.0.1 // Install Futurum.Microsoft.Extensions.DependencyInjection as a Cake Tool #tool nuget:?package=Futurum.Microsoft.Extensions.DependencyInjection&version=2.0.1
Futurum.Microsoft.Extensions.DependencyInjection
A dotnet library that extends Microsoft.Extensions.DependencyInjection by adding support for attribute based registration, modules and startables.
- Easy setup
- Autodiscovery of DependencyInjection registrations, based on attributes and Source Generators
- Support for keyed registrations
- Autodiscovery of DependencyInjection modules, based on attributes and Source Generators
- Autodiscovery of DependencyInjection startables, based on attributes and Source Generators
- Roslyn Analysers to help build your registrations, modules and startables, using best practices
- Tested solution
Easy setup
- Install the NuGet package
- Update program.cs as per here
Program.cs
builder.Services.AddDependencyInjectionFor...();
AddDependencyInjectionFor... (per project containing registrations)
This will be automatically created by the source generator.
e.g.
builder.Services.AddDependencyInjectionForFuturumMicrosoftExtensionsDependencyInjectionSample();
Attribute based registration
You can register services using attributes.
The attributes have been created is a discoverable way. They take the following form:
- RegisterAs{Lifetime}
- RegisterAsKeyed{Lifetime}
- RegisterAs{Lifetime}.AsSelf
- RegisterAs{Lifetime}.AsKeyedSelf
- RegisterAs{Lifetime}.As{ServiceType}
- RegisterAs{Lifetime}.AsKeyed{ServiceType}
- RegisterAs{Lifetime}.AsImplementedInterfaces
- RegisterAs{Lifetime}.AsKeyedImplementedInterfaces
- RegisterAs{Lifetime}.AsImplementedInterfacesAndSelf
- RegisterAs{Lifetime}.AsKeyedImplementedInterfacesAndSelf
- RegisterAs{Lifetime}.AsOpenGeneric
- RegisterAs{Lifetime}.AsKeyedOpenGeneric
There are 3 lifetimes available:
- Singleton
- Scoped
- Transient
NOTE - There is a Roslyn Analyser that will provide information on exactly what registration is taking place. Just hover over the class.
RegisterAs{Lifetime} attribute
This will register the class against the 1 interface the class implements, for the specified lifetime.
NOTE - There is a Roslyn Analyser that will warn you if you use this attribute and the class does not implement exactly 1 interface.
e.g. This will register Service against IService with a Singleton lifetime.
[RegisterAsSingleton]
public class Service : IService
{
}
e.g. This will register Service against IService with a Scoped lifetime.
[RegisterAsScoped]
public class Service : IService
{
}
e.g. This will register Service against IService with a Transient lifetime.
[RegisterAsTransient]
public class Service : IService
{
}
RegisterAsKeyed{Lifetime} attribute
This will register the class against the 1 interface the class implements, for the specified lifetime, with the service key.
NOTE - There is a Roslyn Analyser that will warn you if you use this attribute and the class does not implement exactly 1 interface.
e.g. This will register Service against IService, with a Singleton lifetime, with a "service-key" service key.
[RegisterAsKeyedSingleton("service-key")]
public class Service : IService
{
}
e.g. This will register Service against IService, with a Scoped lifetime, with a "service-key" service key.
[RegisterAsKeyedScoped("service-key")]
public class Service : IService
{
}
e.g. This will register Service against IService, with a Transient lifetime, with a "service-key" service key.
[RegisterAsKeyedTransient("service-key")]
public class Service : IService
{
}
RegisterAs{Lifetime}.AsSelf attribute
This will register the class against itself, for the specified lifetime.
e.g. This will register Service against Service with a Singleton lifetime.
[RegisterAsSingleton.AsSelf]
public class Service
{
}
e.g. This will register Service against Service with a Scoped lifetime.
[RegisterAsScoped.AsSelf]
public class Service
{
}
e.g. This will register Service against Service with a Transient lifetime.
[RegisterAsTransient.AsSelf]
public class Service
{
}
RegisterAs{Lifetime}.AsKeyedSelf attribute
This will register the class against itself, for the specified lifetime, for the specified lifetime, with the service key.
e.g. This will register Service against Service, with a Singleton lifetime, with a "service-key" service key.
[RegisterAsSingleton.AsKeyedSelf("service-key")]
public class Service
{
}
e.g. This will register Service against Service, with a Scoped lifetime, with a "service-key" service key.
[RegisterAsScoped.AsKeyedSelf("service-key")]
public class Service
{
}
e.g. This will register Service against Service, with a Transient lifetime, with a "service-key" service key.
[RegisterAsTransient.AsKeyedSelf("service-key")]
public class Service
{
}
RegisterAs{Lifetime}.As{ServiceType} attribute
This will register the class against the specified interface, for the specified lifetime.
NOTE - There is a Roslyn Analyser that will warn you if you use the class does not implement the specified interface.
e.g. This will register Service against IService1 with a Singleton lifetime.
[RegisterAsSingleton.As<IService2>]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1 with a Scoped lifetime.
[RegisterAsScoped.As<IService2>]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1 with a Transient lifetime.
[RegisterAsTransient.As<IService2>]
public class Service : IService1, IService2
{
}
RegisterAs{Lifetime}.AsKeyed{ServiceType} attribute
This will register the class against the specified interface, for the specified lifetime, with the service key.
NOTE - There is a Roslyn Analyser that will warn you if you use the class does not implement the specified interface.
e.g. This will register Service against IService1, with a Singleton lifetime, with a "service-key" service key.
[RegisterAsSingleton.AsKeyed<IService2>("service-key")]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1, with a Scoped lifetime, with a "service-key" service key.
[RegisterAsScoped.AsKeyed<IService2>("service-key")]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1, with a Transient lifetime, with a "service-key" service key.
[RegisterAsTransient.AsKeyed<IService2>("service-key")]
public class Service : IService1, IService2
{
}
RegisterAs{Lifetime}.AsImplementedInterfaces attribute
This will register the class against all the interfaces it implements directly, for the specified lifetime.
e.g. This will register Service against IService1 and IService2 with a Singleton lifetime.
[RegisterAsSingleton.AsImplementedInterfaces]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1 and IService2 with a Scoped lifetime.
[RegisterAsScoped.AsImplementedInterfaces]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1 and IService2 with a Transient lifetime.
[RegisterAsTransient.AsImplementedInterfaces]
public class Service : IService1, IService2
{
}
RegisterAs{Lifetime}.AsKeyedImplementedInterfaces attribute
This will register the class against all the interfaces it implements directly, for the specified lifetime, with the service key.
e.g. This will register Service against IService1 and IService2, with a Singleton lifetime, with a "service-key" service key.
[RegisterAsSingleton.AsKeyedImplementedInterfaces("service-key")]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1 and IService2, with a Scoped lifetime, with a "service-key" service key.
[RegisterAsScoped.AsKeyedImplementedInterfaces("service-key")]
public class Service : IService1, IService2
{
}
e.g. This will register Service against IService1 and IService2, with a Transient lifetime, with a "service-key" service key.
[RegisterAsTransient.AsKeyedImplementedInterfaces("service-key")]
public class Service : IService1, IService2
{
}
RegisterAs{Lifetime}.AsImplementedInterfacesAndSelf attribute
This will register the class against all the interfaces it implements directly and itself, for the specified lifetime.
e.g. This will register Service against Service, IService1 and IService2 with a Singleton lifetime.
[RegisterAsSingleton.AsImplementedInterfacesAndSelf]
public class Service : IService1, IService2
{
}
e.g. This will register Service against Service, IService1 and IService2 with a Scoped lifetime.
[RegisterAsScoped.AsImplementedInterfacesAndSelf]
public class Service : IService1, IService2
{
}
e.g. This will register Service against Service, IService1 and IService2 with a Transient lifetime.
[RegisterAsTransient.AsImplementedInterfacesAndSelf]
public class Service : IService1, IService2
{
}
RegisterAs{Lifetime}.AsKeyedImplementedInterfacesAndSelf attribute
This will register the class against all the interfaces it implements directly and itself, for the specified lifetime, with the service key.
e.g. This will register Service against Service, IService1 and IService2, with a Singleton lifetime, with a "service-key" service key.
[RegisterAsSingleton.AsKeyedImplementedInterfacesAndSelf("service-key")]
public class Service : IService1, IService2
{
}
e.g. This will register Service against Service, IService1 and IService2, with a Scoped lifetime, with a "service-key" service key.
[RegisterAsScoped.AsKeyedImplementedInterfacesAndSelf("service-key")]
public class Service : IService1, IService2
{
}
e.g. This will register Service against Service, IService1 and IService2, with a Transient lifetime, with a "service-key" service key.
[RegisterAsTransient.AsKeyedImplementedInterfacesAndSelf("service-key")]
public class Service : IService1, IService2
{
}
RegisterAs{Lifetime}.AsOpenGeneric attribute
This will register the an open generic class against an open generic interface, for the specified lifetime.
e.g. This will register Service<T> against IService<T> with a Singleton lifetime.
[RegisterAsSingleton.AsOpenGeneric(ImplementationType = typeof(Service<>), ServiceType = typeof(IService<>))]
public class Service<T> : IService<T>
{
}
e.g. This will register Service<T> against IService<T> with a Scoped lifetime.
[RegisterAsScoped.AsOpenGeneric(ImplementationType = typeof(Service<>), ServiceType = typeof(IService<>))]
public class Service<T> : IService<T>
{
}
e.g. This will register Service<T> against IService<T> with a Transient lifetime.
[RegisterAsTransient.AsOpenGeneric(ImplementationType = typeof(Service<>), ServiceType = typeof(IService<>))]
public class Service<T> : IService<T>
{
}
RegisterAs{Lifetime}.AsKeyedOpenGeneric attribute
This will register the an open generic class against an open generic interface, for the specified lifetime, with the service key.
e.g. This will register Service<T> against IService<T>, with a Singleton lifetime, with a "service-key" service key.
[RegisterAsSingleton.AsKeyedOpenGeneric("service-key", ImplementationType = typeof(Service<>), ServiceType = typeof(IService<>))]
public class Service<T> : IService<T>
{
}
e.g. This will register Service<T> against IService<T>, with a Scoped lifetime, with a "service-key" service key.
[RegisterAsScoped.AsKeyedOpenGeneric("service-key", ImplementationType = typeof(Service<>), ServiceType = typeof(IService<>))]
public class Service<T> : IService<T>
{
}
e.g. This will register Service<T> against IService<T>, with a Transient lifetime, with a "service-key" service key.
[RegisterAsTransient.AsKeyedOpenGeneric("service-key", ImplementationType = typeof(Service<>), ServiceType = typeof(IService<>))]
public class Service<T> : IService<T>
{
}
DuplicateRegistrationStrategy
All registration attributes allow you to specify how to handle duplicate registrations.
- Try - Adds the new registration, if the service hasn't already been registered
- Replace - Removes any existing registration and then adds the new registration
- Add - Adds the new registration, irrespective of if its previously been registered
NOTE - This defaults to Try
Try
NOTE - This is the default behaviour.
e.g. This will register Service against IService with a Singleton lifetime via Try.
[RegisterAsSingleton(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Try)]
public class Service : IService
{
}
e.g. This will register Service against IService with a Scoped lifetime via Try.
[RegisterAsScoped(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Try)]
public class Service : IService
{
}
e.g. This will register Service against IService with a Transient lifetime via Try.
[RegisterAsTransient(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Try)]
public class Service : IService
{
}
Replace
e.g. This will register Service against IService with a Singleton lifetime via Replace.
[RegisterAsSingleton(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Replace)]
public class Service : IService
{
}
e.g. This will register Service against IService with a Scoped lifetime via Replace.
[RegisterAsScoped(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Replace)]
public class Service : IService
{
}
e.g. This will register Service against IService with a Transient lifetime via Replace.
[RegisterAsTransient(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Replace)]
public class Service : IService
{
}
Add
e.g. This will register Service against IService with a Singleton lifetime via Add.
[RegisterAsSingleton(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Add)]
public class Service : IService
{
}
e.g. This will register Service against IService with a Scoped lifetime via Add.
[RegisterAsScoped(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Add)]
public class Service : IService
{
}
e.g. This will register Service against IService with a Transient lifetime via Add.
[RegisterAsTransient(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Add)]
public class Service : IService
{
}
Modules
A module allows you to break up registration into logical units.
Module can either be registered using IModule interface and AddModule extension method, or by using the RegisterAsDependencyInjectionModule attribute.
IModule interface and AddModule extension method
If you use the IModule interface, you must manually register the module using the AddModule extension method.
IModule interface
Implements this interface to create a module.
public class TestModule : IModule
{
public void Load(IServiceCollection services)
{
services.AddSingleton<ITestService, TestService>();
}
}
AddModule extension method
Allows you to register a module.
services.AddModule<TestModule>();
services.AddModule(new TestModule());
Attribute based module
You can also register modules using attributes.
- RegisterAsDependencyInjectionModule attribute
public class Module
{
[RegisterAsDependencyInjectionModule]
public void Load(IServiceCollection services)
{
}
}
public static class Module
{
[RegisterAsDependencyInjectionModule]
public static void Load(IServiceCollection services)
{
}
}
Startables
A startable is resolved at the start of the application lifecycle and is a place to perform actions as soon as the DependencyInjection container is built.
Startable can either be registered using IStartable interface and AddStartable extension method, or by using the RegisterAsDependencyInjectionStartable attribute.
NOTE - The main difference is that using IStartable interface allows you to inject dependencies into the startable, whereas the attribute based approach does not.
IStartable interface and AddStartable extension method
If you use the IStartable interface, you must manually register the startable using the AddStartable extension method.
IStartable interface
Implements this interface to create a startable.
public class TestStartable : IStartable
{
public Task Start()
{
// Do something
}
}
AddStartable extension method
Allows you to register a startable.
services.AddStartable<TestStartable>();
services.AddStartable(new TestStartable());
Attribute based startable
You can also register modules using attributes.
- RegisterAsDependencyInjectionStartable attribute
public class Startable
{
[RegisterAsDependencyInjectionStartable]
public Task StartAsync()
{
// Do something
}
}
public static class Startable
{
[RegisterAsDependencyInjectionStartable]
public static Task StartAsync()
{
// Do something
}
}
Roslyn Analysers
- FMEDI0001 - Invalid Module Parameter
- FMEDI0002 - Missing Module Parameter
- FMEDI0003 - Non empty constructor found on Module
- FMEDI0004 - Non empty constructor found on Startable
- FMEDI0005 - Non async method found on Startable
- FMEDI0006 - Non void method found on Module
- FMEDI0007 - Register ServiceType not implemented by class
- FMEDI0008 - Registration, must only have 1 interface
- FMEDI1000 - Registration information
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on Futurum.Microsoft.Extensions.DependencyInjection:
Package | Downloads |
---|---|
Futurum.WebApiEndpoint
A dotnet library that allows you to build WebApiEndpoints using a vertical slice architecture approach. Provides a structured way to create REST apis in dotnet without controllers. |
|
Futurum.ApiEndpoint
A dotnet library, that is the base for ApiEndpoints in Futurum. |
|
Futurum.WebApiEndpoint.Micro
A dotnet library that allows you to build WebApiEndpoints using a vertical slice architecture approach. Built on dotnet 8 and minimal apis. |
|
Futurum.Core.Polly
Small dotnet library, allowing you to use [Polly](https://github.com/App-vNext/Polly) with Futurum.Core, based on the concepts behind 'Railway Oriented Programming'. |
|
Futurum.WebApiEndpoint.Micro.Core.Extensions
A dotnet library that extends Futurum.WebApiEndpoint.Micro, to make it fully compatible with Futurum.Core. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.1 | 247 | 12/24/2023 |
2.0.0 | 338 | 12/5/2023 |
1.0.11 | 235 | 4/21/2023 |
1.0.10 | 196 | 4/20/2023 |
1.0.9 | 191 | 4/19/2023 |
1.0.8 | 192 | 4/19/2023 |
1.0.7 | 209 | 4/17/2023 |
1.0.6 | 186 | 4/17/2023 |
1.0.5 | 190 | 4/16/2023 |
1.0.4 | 212 | 4/7/2023 |
1.0.3 | 1,227 | 6/3/2022 |
1.0.2 | 2,310 | 2/6/2022 |
1.0.1 | 653 | 2/5/2022 |
1.0.0 | 929 | 1/28/2022 |