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
<PackageReference Include="Bobbysoft.Extensions.DependencyInjection.Decoration" Version="1.1.4" />
paket add Bobbysoft.Extensions.DependencyInjection.Decoration --version 1.1.4
#r "nuget: Bobbysoft.Extensions.DependencyInjection.Decoration, 1.1.4"
// 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 | Versions 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. |
-
net6.0
-
net7.0
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.