Mammoth.Extensions.DependencyInjection
0.1.1-beta0003
See the version list below for details.
dotnet add package Mammoth.Extensions.DependencyInjection --version 0.1.1-beta0003
NuGet\Install-Package Mammoth.Extensions.DependencyInjection -Version 0.1.1-beta0003
<PackageReference Include="Mammoth.Extensions.DependencyInjection" Version="0.1.1-beta0003" />
paket add Mammoth.Extensions.DependencyInjection --version 0.1.1-beta0003
#r "nuget: Mammoth.Extensions.DependencyInjection, 0.1.1-beta0003"
// Install Mammoth.Extensions.DependencyInjection as a Cake Addin #addin nuget:?package=Mammoth.Extensions.DependencyInjection&version=0.1.1-beta0003&prerelease // Install Mammoth.Extensions.DependencyInjection as a Cake Tool #tool nuget:?package=Mammoth.Extensions.DependencyInjection&version=0.1.1-beta0003&prerelease
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 sservice 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 extensins 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 extensins works with all kinds of Service Descriptors (Singleton, Scoped, Transient, Keyed, etc).
Registration Helpers
A series of extentions 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 ServiceDescript exists.
ServiceCollection
- GetServiceDescriptors
- IsServiceRegistered
- IsTransientServiceRegistered
- IsScopedServiceRegistered
- IsSingletonServiceRegistered
ServiceProvider
To use the following extensions you need to build the ServiceProvider using our ServiceProviderFactory
.
It will inject some support services that are used to keep tracks of the registered services.
new HostBuilder().UseServiceProviderFactory(new ServiceProviderFactory());
// - or -
var serviceProvider = ServiceProviderFactory.CreateServiceProvider(serviceCollection);
You can then use the following extensions:
- IsServiceRegistered
- GetAllServices: resolves all keyed and non-keyed services of a given service type
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 ServiceDescriptos 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. |
.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.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.2.0 | 154 | 3/6/2024 |
0.1.3-beta0001 | 99 | 3/6/2024 |
0.1.2 | 124 | 3/4/2024 |
0.1.1 | 129 | 3/4/2024 |
0.1.1-beta0004 | 100 | 3/4/2024 |
0.1.1-beta0003 | 106 | 3/4/2024 |