DependencyInjection.Decorators 1.0.0

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

// Install DependencyInjection.Decorators as a Cake Tool
#tool nuget:?package=DependencyInjection.Decorators&version=1.0.0

DependencyInjection.Decorators

A simple and lightweight way of applying the Decorator Pattern using Asp Net's dependency injection services. This lib is built on top of Microsoft.Extensions.DependencyInjection.Abstractions and allows services to be added to the service provider using a decorator pattern.

Installation

To install this lib, simply install the package from NuGet using the command below.

Install-Package DependencyInjection.Decorators

How To Use

To register services in a decorator pattern you'll use one of the several extension methods for IServiceCollection.

Each service lifetime supported in ServiceLifetime enum has its own method:

  • For Scoped: AddDecoratedScoped;
  • For Transient: AddDecoratedTransient;
  • For Singleton: AddDecoratedSingleton;

Alternatively you can use AddDecorated and pass in the chosen service lifetime.

You'll find both generic and concrete extension methods.

Generic methods will support up to one component and five decorators. For more decorators use the concrete methods.

Examples

// generic methods
services.AddDecoratedScoped<IServiceToDecorate, Decorator3, Decorator2, Decorator1, Component>();
services.AddDecoratedTransient<IServiceToDecorate, Decorator3, Decorator2, Decorator1, Component>();
services.AddDecoratedSingleton<IServiceToDecorate, Decorator3, Decorator2, Decorator1, Component>();

//concrete methods
services.AddDecoratedScoped(serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);
services.AddDecoratedTransient(serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);
services.AddDecoratedSingleton(serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);

//one method to run them all
services.AddDecorated(serviceLifetime, serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);

How Does It Work

The lib uses the factory overload of IServiceCollection.Add method to register the service Type and do its job.

All types passed to AddDecorated methods, except the service type, are registered using the same service lifetime, so there is no need to register them prior to calling AddDecorated.

The factory for the service type, at runtime, instances all services registered, from right to left, using a combination of IServiceProvider and ActivatorUtilities, passing the last instanced service as constructor parameter for the next. This means that the call order, based on the example above, is: Decorator3 → Decorator2 → Decorator1 → Component. (for more details on this matter, check the test project)

Rules Of Thumb

  1. All types are registered by the AddDecorated methods;
  2. All types are registered with the same lifetime;
  3. Instances are created from right to left, each one being inject into the next;
  4. At runtime, calls to the service execute from left to right.

Want to Contribute

If you find bugs and don't have time to submit a PR, please report it using Github isses. On the other hand, if you can submit a PR I'll gladly take a look at it.

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 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. 
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
1.0.0 181 8/25/2022