Bobbysoft.ServiceDecorator 1.1.3

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

// Install Bobbysoft.ServiceDecorator as a Cake Tool
#tool nuget:?package=Bobbysoft.ServiceDecorator&version=1.1.3                

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.3 101 10/11/2024