@@ -52,41 +52,46 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5252
5353 private static string GenerateRegistrationsSource ( MethodModel method , EquatableArray < ServiceRegistrationModel > registrations )
5454 {
55- var registrationsCode = string . Join ( "\n " , registrations . Select ( registration =>
56- {
57- if ( registration . IsOpenGeneric )
58- {
59- return $ " .Add{ registration . Lifetime } (typeof({ registration . ServiceTypeName } ), typeof({ registration . ImplementationTypeName } ))";
60- }
61- else
55+ var registrationsCode = string . Concat ( registrations
56+ . Select ( registration =>
6257 {
63- if ( registration . ResolveImplementation )
58+ if ( registration . IsOpenGeneric )
6459 {
65- return $ " .Add{ registration . Lifetime } < { registration . ServiceTypeName } >(s => s.GetRequiredService< { registration . ImplementationTypeName } >( ))";
60+ return $ ".Add{ registration . Lifetime } (typeof( { registration . ServiceTypeName } ), typeof( { registration . ImplementationTypeName } ))";
6661 }
6762 else
6863 {
69- var addMethod = registration . KeySelector != null
70- ? $ "AddKeyed{ registration . Lifetime } "
71- : $ "Add{ registration . Lifetime } ";
72-
73- var keySelectorInvocation = registration . KeySelectorType switch
64+ if ( registration . ResolveImplementation )
7465 {
75- KeySelectorType . GenericMethod => $ "{ registration . KeySelector } <{ registration . ImplementationTypeName } >()",
76- KeySelectorType . Method => $ "{ registration . KeySelector } (typeof({ registration . ImplementationTypeName } ))",
77- KeySelectorType . TypeMember => $ "{ registration . ImplementationTypeName } .{ registration . KeySelector } ",
78- _ => null
79- } ;
80-
81- return $ " .{ addMethod } <{ registration . ServiceTypeName } , { registration . ImplementationTypeName } >({ keySelectorInvocation } )";
66+ return $ ".Add{ registration . Lifetime } <{ registration . ServiceTypeName } >(s => s.GetRequiredService<{ registration . ImplementationTypeName } >())";
67+ }
68+ else
69+ {
70+ var addMethod = registration . KeySelector != null
71+ ? $ "AddKeyed{ registration . Lifetime } "
72+ : $ "Add{ registration . Lifetime } ";
73+
74+ var keySelectorInvocation = registration . KeySelectorType switch
75+ {
76+ KeySelectorType . GenericMethod => $ "{ registration . KeySelector } <{ registration . ImplementationTypeName } >()",
77+ KeySelectorType . Method => $ "{ registration . KeySelector } (typeof({ registration . ImplementationTypeName } ))",
78+ KeySelectorType . TypeMember => $ "{ registration . ImplementationTypeName } .{ registration . KeySelector } ",
79+ _ => null
80+ } ;
81+
82+ return $ ".{ addMethod } <{ registration . ServiceTypeName } , { registration . ImplementationTypeName } >({ keySelectorInvocation } )";
83+ }
8284 }
83- }
84- } ) ) ;
85+ } )
86+ . Select ( line => $ " \n { line } " ) ) ;
8587
8688 var returnType = method . ReturnsVoid ? "void" : "IServiceCollection" ;
87-
8889 var namespaceDeclaration = method . Namespace is null ? "" : $ "namespace { method . Namespace } ;";
8990
91+ var methodBody = registrations . Count == 0 && method . ReturnsVoid
92+ ? ""
93+ : $$ """ {{ ( method . ReturnsVoid ? "" : "return " ) }} {{ method . ParameterName }} {{ registrationsCode }} ;""" ;
94+
9095 var source = $$ """
9196 using Microsoft.Extensions.DependencyInjection;
9297
@@ -96,8 +101,7 @@ private static string GenerateRegistrationsSource(MethodModel method, EquatableA
96101 {
97102 {{ method . MethodModifiers }} {{ returnType }} {{ method . MethodName }} ({{ ( method . IsExtensionMethod ? "this" : "" ) }} IServiceCollection {{ method . ParameterName }} )
98103 {
99- {{ ( method . ReturnsVoid ? "" : "return " ) }} {{ method . ParameterName }}
100- {{ registrationsCode . Trim ( ) }} ;
104+ {{ methodBody }}
101105 }
102106 }
103107 """ ;
0 commit comments