The Decorate extension method allows you to wrap an existing service registration with a decorator. You can decorate using a simple type-to-type mapping or a factory delegate that provides access to the inner service and the IServiceProvider.
When multiple decorators are applied, they wrap each other in the order they were registered (e.g., Decorator2 -> Decorator1 -> OriginalService).
var collection = new ServiceCollection();
// 1. Add the base service
collection.AddSingleton<IDecoratedService, Decorated>();
// 2. Decorate with a simple type
collection.Decorate<IDecoratedService, Decorator>();
// 3. Decorate using a factory delegate (allows injecting other services from the provider)
collection.Decorate<IDecoratedService>((inner, provider) =>
new OtherDecorator(inner, provider.GetRequiredService<IService>()));
var serviceProvider = collection.BuildServiceProvider();
// Resolving IDecoratedService returns: OtherDecorator -> Decorator -> Decorated
var instance = serviceProvider.GetRequiredService<IDecoratedService>();