Bobbysoft.Extensions.DependencyInjection.Decoration 1.1.4

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

// Install Bobbysoft.Extensions.DependencyInjection.Decoration as a Cake Tool
#tool nuget:?package=Bobbysoft.Extensions.DependencyInjection.Decoration&version=1.1.4                

Bobbysoft.Extensions.DependencyInjection.ServiceDecoration

ServiceDecorator

This light weight class library is used to decorate already dependency injected classes inside the Microsoft.Extensions.DependencyInjection.IServiceCollection.


The quick overview

using Bobbysoft.Extensions.DependencyInjection;

public void DecorateServices(IServiceCollection services)
{

  // ***ServiceDecorator.Decorate is the extension method added with this Library
  services.Decorate<ILogger>(
    subject => new LoggerDecorator(subject));
}

using Bobbysoft.Extensions.DependencyInjection;

public void DecorateServices(IServiceCollection services)
{

  // ***ServiceDecorator.Decorate is the extension method added with this Library
  services.Decorate<ILogger>(
    (subject, provider) => new LoggerDecorator(subject, provider.GetRequiredService<Emailer>()));
}

Step 1: make sure you know what a decorator is

Step 2: decide which class to decorate

I choose the SeriLogger from SeriLog that is registered with the ILogger interface. It does not matter if the interface is an abstract class interface or an c#-Interface-Interface I could have picked any of the two for this tutorial.

Step 3: write a decorator to the chosen abstraction

internal class LoggerDecorator : ILogger
{
    private readonly ILogger _logger;
    public LoggerDecorator(ILogger logger)
    {
        _logger = logger;
    }

    public void Write(LogEvent logEvent)
    {
        DecorateLogEvent(logEvent);
        _logger.Write(logEvent);
    }

    private void DecorateLogEvent(LogEvent logEvent)
    {
        // Decorate your logEvent here
    }
}

Step 4: Register both Service (abstraction) and implementation in the IServiceCollection

public void ConfigureServices(IServiceCollection services)
{

  services.AddLogging(loggingBuilder =>
    loggingBuilder.AddSerilog(dispose: true));

      // Other services ...
}

Step 5: Decorate the ServiceCollection from the outside of the IServiceCollection

using Bobbysoft.Extensions.DependencyInjection;

public void DecorateServices(IServiceCollection services)
{

  // ***ServiceDecorator.Decorate is the extension method added with this Library
  services.Decorate<ILogger>(
    subject => new LoggerDecorator(subject));
}

The other way - with multiple dependencies

Add a modified decorator

internal class LoggerDecorator : ILogger
{
    private readonly ILogger _logger;
    private readonly Emailer _emailer;

    // We are adding a second dependency Emailer
    public LoggerDecorator(ILogger logger, Emailer emailer)
    {
        _logger = logger;
        _emailer = emailer;
    }

    public void Write(LogEvent logEvent)
    {
        DecorateLogEvent(logEvent);
        _emailer.Send(logEvent);
        _logger.Write(logEvent);
    }

    private void DecorateLogEvent(LogEvent logEvent)
    {
        // Decorate your logEvent here
    }
}

Register both implementations services

using Bobbysoft.Extensions.DependencyInjection;

public void ConfigureServices(IServiceCollection services)
{

  services.AddLogging(loggingBuilder =>
    loggingBuilder.AddSerilog(dispose: true));

  services.AddTransient<Emailer>();
      // Other services ...
}

public void DecorateServices(IServiceCollection services)
{

  // ***ServiceDecorator.Decorate is the extension method added with this Library
  services.Decorate<ILogger>(
    (subject, provider) => new LoggerDecorator(subject, provider.GetService<Emailer>()));
}

If in doubt look at the different examples in the test project

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 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. 
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.1.4 81 10/11/2024
1.1.2 137 3/7/2024
1.1.1 280 2/20/2023
1.1.0 245 2/20/2023
1.0.0 313 1/20/2023 1.0.0 is deprecated because it has critical bugs.