using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
public class LoggingDecorator : IDecorator
public async Task OnInvoke(Call call)
Console.WriteLine("Will do some work!");
Console.WriteLine("Work is finished!");
public class CachingDecorator : IDecorator
public async Task OnInvoke(Call call)
Console.WriteLine("Stuffed {0} in the cache!", call.ReturnValue);
string SomeOtherWork(string s);
public class Worker : IWorker
[Decorate(typeof(LoggingDecorator))]
[Decorate(typeof(CachingDecorator))]
Console.WriteLine("Working...");
return DateTime.Now.ToString();
[Decorate(typeof(LoggingDecorator))]
[Decorate(typeof(CachingDecorator))]
public string SomeOtherWork(string s)
Console.WriteLine("Some Other Working...");
var serviceProvider = new ServiceCollection()
.AddTransient<LoggingDecorator>()
.AddTransient<CachingDecorator>()
.AddSingleton<IWorker, Worker>()
var worker = serviceProvider.GetRequiredService<IWorker>();
worker.SomeOtherWork("Fred");
worker.SomeOtherWork("Billy");
worker.SomeOtherWork("Bob");