Extends Microsoft.Extensions.DependencyInjection.IServiceCollection to easy decorate an already registered service implementation.
$ dotnet add package Bobbysoft.Extensions.DependencyInjection.DecorationThis light weight class library is used to decorate already dependency injected classes inside the Microsoft.Extensions.DependencyInjection.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));
}
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>()));
}
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.
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
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
loggingBuilder.AddSerilog(dispose: true));
// Other services ...
}
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));
}
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