Skip to content

Commit 0a6ec0e

Browse files
committed
Skip cached lambdas for dependent services
1 parent ae22771 commit 0a6ec0e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

components/Extensions.DependencyInjection/CommunityToolkit.Extensions.DependencyInjection.SourceGenerators/ServiceProviderGenerator.Execute.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ public static CompilationUnitSyntax GetSyntax(ServiceCollectionInfo info)
199199
// Prepare the method name, either AddSingleton or AddTransient
200200
string registrationMethod = $"Add{serviceInfo.RegistrationKind}";
201201

202-
// Special case when the service is a singleton and no dependent services are present, just use eager instantiation instead:
202+
// Special case when the service is a singleton and no dependent services are present, we can use the parameterless constructor
203203
//
204-
// global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton(<PARAMETER_NAME>, typeof(<ROOT_SERVICE_TYPE>), new <IMPLEMENTATION_TYPE>());
204+
// global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton(<PARAMETER_NAME>, typeof(<ROOT_SERVICE_TYPE>), static _ => new <IMPLEMENTATION_TYPE>());
205205
if (serviceInfo.RegistrationKind == ServiceRegistrationKind.Singleton && constructorArguments.Count == 0)
206206
{
207207
registrationStatements.Add(
@@ -246,7 +246,7 @@ public static CompilationUnitSyntax GetSyntax(ServiceCollectionInfo info)
246246
{
247247
// Register the main implementation type:
248248
//
249-
// global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.<REGISTRATION_METHOD>(<PARAMETER_NAME>, typeof(<DEPENDENT_SERVICE_TYPE>), static services => global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredServices<ROOT_SERVICE_TYPE>(services));
249+
// global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.<REGISTRATION_METHOD>(<PARAMETER_NAME>, typeof(<DEPENDENT_SERVICE_TYPE>), new global::System.Action<global::Microsoft.Extensions.DependencyInjection.IServiceCollection, object>(global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredServices<ROOT_SERVICE_TYPE>));
250250
registrationStatements.Add(
251251
ExpressionStatement(
252252
InvocationExpression(
@@ -258,16 +258,17 @@ public static CompilationUnitSyntax GetSyntax(ServiceCollectionInfo info)
258258
Argument(IdentifierName(info.Method.ServiceCollectionParameterName)),
259259
Argument(TypeOfExpression(IdentifierName(dependentServiceType))),
260260
Argument(
261-
SimpleLambdaExpression(Parameter(Identifier("services")))
262-
.AddModifiers(Token(SyntaxKind.StaticKeyword))
263-
.WithExpressionBody(
264-
InvocationExpression(
265-
MemberAccessExpression(
266-
SyntaxKind.SimpleMemberAccessExpression,
267-
IdentifierName("global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions"),
268-
GenericName(Identifier("GetRequiredService"))
269-
.AddTypeArgumentListArguments(IdentifierName(rootServiceTypeName))))
270-
.AddArgumentListArguments(Argument(IdentifierName("services"))))))));
261+
ObjectCreationExpression(
262+
GenericName("global::System.Action")
263+
.AddTypeArgumentListArguments(
264+
IdentifierName("global::Microsoft.Extensions.DependencyInjection.IServiceCollection"),
265+
PredefinedType(Token(SyntaxKind.ObjectKeyword))))
266+
.AddArgumentListArguments(Argument(
267+
MemberAccessExpression(
268+
SyntaxKind.SimpleMemberAccessExpression,
269+
IdentifierName("global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions"),
270+
GenericName(Identifier("GetRequiredService"))
271+
.AddTypeArgumentListArguments(IdentifierName(rootServiceTypeName)))))))));
271272
}
272273
}
273274

0 commit comments

Comments
 (0)