Mammoth.Extensions.DependencyInjection
0.5.2
See the version list below for details.
dotnet add package Mammoth.Extensions.DependencyInjection --version 0.5.2
NuGet\Install-Package Mammoth.Extensions.DependencyInjection -Version 0.5.2
<PackageReference Include="Mammoth.Extensions.DependencyInjection" Version="0.5.2" />
<PackageVersion Include="Mammoth.Extensions.DependencyInjection" Version="0.5.2" />
<PackageReference Include="Mammoth.Extensions.DependencyInjection" />
paket add Mammoth.Extensions.DependencyInjection --version 0.5.2
#r "nuget: Mammoth.Extensions.DependencyInjection, 0.5.2"
#addin nuget:?package=Mammoth.Extensions.DependencyInjection&version=0.5.2
#tool nuget:?package=Mammoth.Extensions.DependencyInjection&version=0.5.2
Mammoth.Extensions.DependencyInjection
Build Status
Introduction
This package provides a set of extensions for the Microsoft.Extensions.DependencyInjection
package.
It is intended to use with Microsoft.Extensions.DependencyInjection 8.0.0
and greater, because it relies heavily on
the keyed services support introduced in that version.
Installation
dotnet add package Mammoth.Extensions.DependencyInjection
Usage
Decorator
The Decorator
extension is used to decorate an existing service with a new implementation.
This is useful when you want to add new functionality to an existing service without modifying the original implementation.
The following example demonstrates how to decorate an existing service with a new implementation.
The current implementation requires that the service to be decorated implements an interface.
public interface ITestService { }
public class TestService : ITestService { }
public class DecoratorService1 : ITestService
{
private readonly ITestService _service;
public DecoratorService1(ITestService service)
{
_service = service;
}
}
public class DecoratorService2 : ITestService
{
private readonly ITestService _service;
public DecoratorService2(ITestService service)
{
_service = service;
}
}
services.AddTransient<ITestService, TestService>();
services.Decorate<IService, DecoratorService1>(); // innermost decorator
services.Decorate<IService, DecoratorService2>(); // outermost decorator
This extension works with all kinds of Service Descriptors (Singleton, Scoped, Transient, Keyed, etc).
DependsOn (requires Keyed Services support)
The DependsOn
registration extensions are used to register a service that depends on specific instances of other services.
The following example demonstrates how to register a service that depends on specific instances of other services.
public interface ITestService { }
public class TestService : ITestService { }
public class DependentService
{
private readonly ITestService _service;
public DependentService(ITestService service)
{
_service = service;
}
}
services.AddTransient<ITestService, TestService>("one");
services.AddTransient<ITestService, TestService>("two");
services.AddTransient<DependentService>(dependsOn: new Dependency[] {
Parameter.ForKey("service").Eq("one")
});
Internally, the DependsOn
extensions create a factory function that resolves the required services and creates the dependent service instances.
The current syntax is limited to some common use case and it's very similar to the one offered by Castle.Windsor from which we took inspiration:
inject a specific instance of a service that will be resolved:
services.AddTransient<DependentService>(dependsOn: new Dependency[] { Parameter.ForKey("service").Eq("one") });
inject a value:
services.AddTransient<DependentService>(dependsOn: new Dependency[] { Dependency.OnValue("dep", "val1") });
This extension works with all kinds of Service Descriptors (Singleton, Scoped, Transient, Keyed, etc).
Registration Helpers
A series of extensions and helpers methods to check is a component was registered and to inspect the assemblies looking for services to be registered.
Checks
Helper methods to check if a service was registered or a ServiceDescriptor exists.
ServiceCollection
GetServiceDescriptors
: returns all the ServiceDescriptors (keyed and non keyed) of a given service type.IsServiceRegistered
: checks whether the specified service type is registered in the service collection (keyed or not).IsKeyedServiceRegistered
: checks whether the specified service type is registered as keyed in the service collection.IsTransientServiceRegistered
IsScopedServiceRegistered
IsSingletonServiceRegistered
IsKeyedTransientServiceRegistered
IsKeyedScopedServiceRegistered
IsKeyedSingletonServiceRegistered
ServiceProvider
To use the following extensions you need to build the ServiceProvider using our ServiceProviderFactory
.
It will inject support services used to detect incorrect usage of Transient Disposable services and keep track of registered services.
new HostBuilder().UseServiceProviderFactory(new ServiceProviderFactory(new ExtendedServiceProviderOptions()));
// - or -
var serviceProvider = ServiceProviderFactory.CreateServiceProvider(serviceCollection, new ExtendedServiceProviderOptions());
Detect incorrect usage of Transient Disposables
To detect incorrect usage of Transient Disposable when they are resolved by Root Scope, create the service provider with:
new HostBuilder().UseServiceProviderFactory(new ServiceProviderFactory(
new ExtendedServiceProviderOptions
{
DetectIncorrectUsageOfTransientDisposables = true
AllowSingletonToResolveTransientDisposables = true
ThrowOnOpenGenericTransientDisposable = true
}));
WARNING: use this feature only in debug and development build, because it has a performance impact and internally uses reflection to access internal Service Provider implementation (it can be fragile).
Implementing these validity checks during the ServiceProvider build phase was a bit complicated and would have required to build a new ServiceProvider from scratch (due to the fact that the actual classes are sealed and there are no extension points to use), Thus we opted for a more straightforward approach:
- Patch all the ServiceDescriptors with new ones that tack the resolution context and throws an exception when the Transient Disposable service is resolved by the root scope.
Limitations:
- Open generic resolution context cannot be tracked! They will NOT result in errors if they are disposable and registered as transient when resolved by the root scope.
Options:
- AllowSingletonToResolveTransientDisposables: allow singletons to resolve transient disposable services (if false an exception will be thrown).
- ThrowOnOpenGenericTransientDisposable: throw an exception when an open generic transient disposable service is registered (we cannot track open generics, better to use all closed types to avoid memory leaks).
IsRegistered extension methods
You can use the following IServiceProvider
extension methods:
GetAllServices
: resolves all keyed and non-keyed services of a given service type.IsServiceRegistered
: checks whether the specified service type is registered in the service provider (keyed or non-keyed).IsKeyedServiceRegistered
: checks whether the specified service type is registered as keyed in the service provider.IsTransientServiceRegistered
: checks whether the specified service type is registered as transient in the service provider (non keyed services).IsScopedServiceRegistered
: checks whether the specified service type is registered as scoped in the service provider (non keyed services).IsSingletonServiceRegistered
: checks whether the specified service type is registered as singleton in the service provider (non keyed services).IsKeyedTransientServiceRegistered
: checks whether the specified service type is registered as transient in the service provider (keyed services).IsKeyedScopedServiceRegistered
: checks whether the specified service type is registered as scoped in the service provider (keyed services).IsKeyedSingletonServiceRegistered
: checks whether the specified service type is registered as singleton in the service provider (keyed services).
Inspectors
The AssemblyInspector
class is used to inspect the assemblies looking for services to be registered.
It is once again inspired by the syntax used in Castle.Windsor to inspect and register services.
It looks for classes and offers a series of methods that are pretty self explanatory to output ServiceDescriptors that will be registered in the ServiceCollection.
It also supports the DependsOn
registration extensions:
serviceCollection.Add(
new AssemblyInspector()
.FromAssemblyContaining<ServiceWithKeyedDep>()
.BasedOn<ServiceWithKeyedDep>()
.WithServiceSelf()
.LifestyleSingleton(dependsOn: new Dependency[]
{
Parameter.ForKey("keyedService").Eq("one")
})
);
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. net9.0 was computed. 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. |
.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 was computed. |
.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.Bcl.AsyncInterfaces (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- System.Reflection.Emit (>= 4.7.0)
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 |
---|---|---|
0.5.7 | 10 | 7/2/2025 |
0.5.7-beta0002 | 10 | 7/2/2025 |
0.5.7-beta0001 | 9 | 7/2/2025 |
0.5.6 | 493 | 3/26/2025 |
0.5.6-beta0003 | 472 | 3/25/2025 |
0.5.6-beta0002 | 476 | 3/25/2025 |
0.5.6-beta0001 | 468 | 3/25/2025 |
0.5.5 | 405 | 3/24/2025 |
0.5.4 | 110 | 3/21/2025 |
0.5.3 | 113 | 3/21/2025 |
0.5.2 | 151 | 3/20/2025 |
0.5.2-beta0001 | 139 | 3/20/2025 |
0.5.1 | 151 | 3/19/2025 |
0.5.0 | 147 | 3/17/2025 |
0.4.0 | 140 | 3/17/2025 |
0.4.0-beta0001 | 138 | 3/17/2025 |
0.3.0 | 122 | 1/24/2025 |
0.3.0-beta0004 | 72 | 1/24/2025 |
0.2.0 | 559 | 3/6/2024 |
0.1.3-beta0001 | 109 | 3/6/2024 |
0.1.2 | 139 | 3/4/2024 |
0.1.1 | 150 | 3/4/2024 |
0.1.1-beta0004 | 114 | 3/4/2024 |
0.1.1-beta0003 | 116 | 3/4/2024 |