@@ -603,6 +603,66 @@ public partial void ProcessServices( global::Microsoft.Extensions.DependencyInje
603603 Assert . Equal ( expected , results . GeneratedTrees [ 1 ] . ToString ( ) ) ;
604604 }
605605
606+ [ Fact ]
607+ public void AddServicesWithDecorator ( )
608+ {
609+ var services = """
610+ namespace GeneratorTests;
611+
612+ public interface ICommandHandler<T> { }
613+ public class CommandHandlerDecorator<T>(ICommandHandler<T> inner) : ICommandHandler<T>;
614+
615+ public class SpecificHandler1 : ICommandHandler<string>;
616+ public class SpecificHandler2 : ICommandHandler<long>;
617+ """ ;
618+
619+ var source = """
620+ using ServiceScan.SourceGenerator;
621+ using Microsoft.Extensions.DependencyInjection;
622+
623+ namespace GeneratorTests;
624+
625+ public static partial class ServiceCollectionExtensions
626+ {
627+ [GenerateServiceRegistrations(AssignableTo = typeof(ICommandHandler<>), CustomHandler = nameof(AddDecoratedHandler))]
628+ public static partial IServiceCollection AddHandlers(this IServiceCollection services);
629+
630+ private static void AddDecoratedHandler<THandler, TCommand>(this IServiceCollection services)
631+ where THandler : class, ICommandHandler<TCommand>
632+ {
633+ // Add handler itself to DI
634+ services.AddScoped<THandler>();
635+
636+ // Register decorated handler as ICommandHandler
637+ services.AddScoped<ICommandHandler<TCommand>>(s => new CommandHandlerDecorator<TCommand>(s.GetRequiredService<THandler>()));
638+ }
639+ }
640+ """ ;
641+
642+
643+ var compilation = CreateCompilation ( source , services ) ;
644+
645+ var results = CSharpGeneratorDriver
646+ . Create ( _generator )
647+ . RunGenerators ( compilation )
648+ . GetRunResult ( ) ;
649+
650+ var expected = $$ """
651+ namespace GeneratorTests;
652+
653+ public static partial class ServiceCollectionExtensions
654+ {
655+ public static partial global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddHandlers(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
656+ {
657+ AddDecoratedHandler<global::GeneratorTests.SpecificHandler1, string>(services);
658+ AddDecoratedHandler<global::GeneratorTests.SpecificHandler2, long>(services);
659+ return services;
660+ }
661+ }
662+ """ ;
663+ Assert . Equal ( expected , results . GeneratedTrees [ 1 ] . ToString ( ) ) ;
664+ }
665+
606666 private static Compilation CreateCompilation ( params string [ ] source )
607667 {
608668 var path = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ! ;
0 commit comments