From 319ee7f129894e4c9095d4891554a1b9d387f893 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Wed, 3 Apr 2024 19:35:15 +0300 Subject: [PATCH] #45 Ability to register composition roots with fewer API calls - refactoring --- .../Core/ApiInvocationProcessor.cs | 58 ++++++++++++------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs b/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs index 9e1d293fb..de0fa0e5e 100644 --- a/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs +++ b/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs @@ -156,7 +156,13 @@ public void ProcessInvocation( var tagArguments = invocation.ArgumentList.Arguments.SkipWhile((arg, i) => arg.NameColon?.Name.Identifier.Text != "tags" && i < 2); var tags = BuildTags(semanticModel, tagArguments); VisitBind(metadataVisitor, semanticModel, invocation, tags, genericName, cancellationToken); - VisitRoot(arguments, tags.FirstOrDefault(), metadataVisitor, semanticModel, invocation, invocationComments, genericName, cancellationToken); + if (genericName.TypeArgumentList.Arguments is not [{ } rootBindType]) + { + return; + } + + var rootBindSymbol = semanticModel.GetTypeSymbol(rootBindType, cancellationToken); + VisitRoot(arguments, tags.FirstOrDefault(), metadataVisitor, semanticModel, invocation, invocationComments, rootBindSymbol); break; case nameof(IBinding.To): @@ -196,7 +202,13 @@ public void ProcessInvocation( break; case nameof(IConfiguration.Root): - VisitRoot(arguments, metadataVisitor, semanticModel, invocation, invocationComments, genericName, cancellationToken); + if (genericName.TypeArgumentList.Arguments is not [{ } rootType]) + { + return; + } + + var rootSymbol = semanticModel.GetTypeSymbol(rootType, cancellationToken); + VisitRoot(arguments, metadataVisitor, semanticModel, invocation, invocationComments, rootSymbol); break; case nameof(IConfiguration.TypeAttribute): @@ -252,19 +264,19 @@ public void ProcessInvocation( } } - private static void VisitRoot(IArguments arguments, IMetadataVisitor metadataVisitor, SemanticModel semanticModel, InvocationExpressionSyntax invocation, IReadOnlyCollection invocationComments, GenericNameSyntax genericName, CancellationToken cancellationToken) + private static void VisitRoot( + IArguments arguments, + IMetadataVisitor metadataVisitor, + SemanticModel semanticModel, + InvocationExpressionSyntax invocation, + IReadOnlyCollection invocationComments, + ITypeSymbol rootSymbol) { - if (genericName.TypeArgumentList.Arguments is not [{ } rootType]) - { - return; - } - - var rootSymbol = semanticModel.GetTypeSymbol(rootType, cancellationToken); var rootArgs = arguments.GetArgs(invocation.ArgumentList, "name", "tag", "kind"); var name = rootArgs[0] is { } nameArg ? semanticModel.GetConstantValue(nameArg.Expression) ?? "" : ""; var tag = rootArgs[1] is { } tagArg ? semanticModel.GetConstantValue(tagArg.Expression) : null; var kind = rootArgs[2] is { } kindArg ? semanticModel.GetConstantValue(kindArg.Expression) : RootKinds.Default; - metadataVisitor.VisitRoot(new MdRoot(rootType, semanticModel, rootSymbol, name, new MdTag(0, tag), kind, invocationComments)); + metadataVisitor.VisitRoot(new MdRoot(invocation, semanticModel, rootSymbol, name, new MdTag(0, tag), kind, invocationComments)); } private static void VisitRoot( @@ -274,29 +286,33 @@ private static void VisitRoot( SemanticModel semanticModel, InvocationExpressionSyntax invocation, IReadOnlyCollection invocationComments, - GenericNameSyntax genericName, - CancellationToken cancellationToken) + ITypeSymbol rootSymbol) { - if (genericName.TypeArgumentList.Arguments is not [{ } rootType]) - { - return; - } - tag ??= new MdTag(0, default); - var rootSymbol = semanticModel.GetTypeSymbol(rootType, cancellationToken); var rootArgs = arguments.GetArgs(invocation.ArgumentList, "name", "kind"); var name = rootArgs[0] is { } nameArg ? semanticModel.GetConstantValue(nameArg.Expression) ?? "" : ""; var kind = rootArgs[1] is { } kindArg ? semanticModel.GetConstantValue(kindArg.Expression) : RootKinds.Default; - metadataVisitor.VisitRoot(new MdRoot(rootType, semanticModel, rootSymbol, name, tag, kind, invocationComments)); + metadataVisitor.VisitRoot(new MdRoot(invocation, semanticModel, rootSymbol, name, tag, kind, invocationComments)); } - private static void VisitBind(IMetadataVisitor metadataVisitor, SemanticModel semanticModel, InvocationExpressionSyntax invocation, GenericNameSyntax genericName, CancellationToken cancellationToken) + private static void VisitBind( + IMetadataVisitor metadataVisitor, + SemanticModel semanticModel, + InvocationExpressionSyntax invocation, + GenericNameSyntax genericName, + CancellationToken cancellationToken) { var tags = BuildTags(semanticModel, invocation.ArgumentList.Arguments); VisitBind(metadataVisitor, semanticModel, invocation, tags, genericName, cancellationToken); } - private static void VisitBind(IMetadataVisitor metadataVisitor, SemanticModel semanticModel, InvocationExpressionSyntax invocation, ImmutableArray tags, GenericNameSyntax genericName, CancellationToken cancellationToken) + private static void VisitBind( + IMetadataVisitor metadataVisitor, + SemanticModel semanticModel, + InvocationExpressionSyntax invocation, + ImmutableArray tags, + GenericNameSyntax genericName, + CancellationToken cancellationToken) { var contractTypes = genericName.TypeArgumentList.Arguments; foreach (var contractType in contractTypes)