This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddDecorator<IEmailMessageSender, EmailMessageSenderWithRetryDecorator>( | |
decorateeServices => | |
{ | |
decorateeServices.AddDecorator<IEmailMessageSender, SecondEmailSenderDecorator>( | |
decorateeServices2 => | |
decorateeServices2.AddScoped<IEmailMessageSender, SmtpEmailMessageSender>()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddDecorator<IEmailMessageSender, EmailMessageSenderWithRetryDecorator>( | |
decorateeServices => | |
{ | |
decorateeServices.AddScoped<IEmailMessageSender, SmtpEmailMessageSender>(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var services = new ServiceCollection(); | |
services.AddDecorator<IEmailMessageSender, EmailMessageSenderWithRetryDecorator>( | |
decorateeServices => decorateeServices.AddScoped<IEmailMessageSender, SmtpEmailMessageSender>()); | |
var serviceProvider = services.BuildServiceProvider(); | |
var emailSender = serviceProvider.GetRequiredService<IEmailMessageSender>(); | |
emailSender.SendMessage(new EmailMessage { Body = "Hello, decorator!" }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new EmailMessageSenderWithRetryDecorator( | |
innerSender: new SmtpEmailMessageSender(/*dependencies*/) | |
/*, other dependencies*/); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmailMessageSenderWithRetryDecorator : IEmailMessageSender | |
{ | |
private readonly IEmailMessageSender innerSender; | |
// Hardcoded for simplicity. | |
private const int RetryCount = 3; | |
private const int DelayBetweenRetriesMs = 1000; | |
public EmailMessageSenderWithRetryDecorator( | |
IEmailMessageSender innerSender |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SmtpEmailMessageSender : IEmailMessageSender | |
{ | |
public void SendMessage(EmailMessage message) | |
{ | |
// Put your real implementation here. | |
Console.WriteLine(message.Body); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IEmailMessageSender | |
{ | |
void SendMessage(EmailMessage message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Microsoft.Extensions.DependencyInjection | |
{ | |
public static class DecoratorServiceCollectionExtensions | |
{ | |
public static void AddDecorator<TService, TDecorator>( | |
this IServiceCollection serviceCollection, |