@@ -10,6 +10,8 @@ class CompositionBuilder(
1010 IBuilder < CompositionCode , Lines > classDiagramBuilder ,
1111 IOverridesRegistry overridesRegistry ,
1212 IRegistry < int > bindingsRegistry ,
13+ IGraphWalker < RootArgsContext , ImmutableArray < Dependency > > graphWalker ,
14+ IGraphVisitor < RootArgsContext , ImmutableArray < Dependency > > rootArgsVisitor ,
1315 CancellationToken cancellationToken )
1416 : IBuilder < DependencyGraph , CompositionCode >
1517{
@@ -27,68 +29,81 @@ public CompositionCode Build(DependencyGraph graph)
2729 break ;
2830 }
2931
32+ var typeDescription = typeResolver . Resolve ( graph . Source , root . Injection . Type ) ;
33+ var processedRoot = root with { TypeDescription = typeDescription } ;
34+ if ( typeDescription . TypeArgs . Count > 0 )
35+ {
36+ processedRoot = processedRoot with { Kind = processedRoot . Kind & ~ RootKinds . Light } ;
37+ }
38+
39+ IEnumerable < VarDeclaration > args ;
3040 var lines = new Lines ( ) ;
3141 using var rootToken = varsMap . Root ( lines ) ;
32- var ctx = new RootContext ( graph , root , varsMap , lines ) ;
33- var rootVarInjection = rootBuilder ( ) . Build ( ctx ) ;
34- var isThreadSafeRoot = ctx . LockIsInUse || graph . Source . Hints . IsThreadSafeEnabled && varsMap . IsThreadSafe ;
35- if ( root . IsStatic )
42+ if ( root . Source . Kind . HasFlag ( RootKinds . Light ) && typeDescription . TypeArgs . Count == 0 )
3643 {
37- if ( isThreadSafeRoot || overridesRegistry . GetOverrides ( root ) . Any ( ) )
38- {
39- // resolveLock local field
40- var newLines = new Lines ( ) ;
41- newLines . AppendLine ( new Line ( int . MinValue , "#if NET9_0_OR_GREATER" ) ) ;
42- newLines . AppendLine ( $ "var { Names . PerResolveLockFieldName } = new { Names . LockTypeName } ();") ;
43- newLines . AppendLine ( new Line ( int . MinValue , "#else" ) ) ;
44- newLines . AppendLine ( $ "var { Names . PerResolveLockFieldName } = new { Names . ObjectTypeName } ();") ;
45- newLines . AppendLine ( new Line ( int . MinValue , "#endif" ) ) ;
46- newLines . AppendLines ( ctx . Lines ) ;
47- lines = newLines ;
48- ctx = ctx with { Lines = lines } ;
49- }
44+ var rootArgsContext = new RootArgsContext ( varsMap , new List < VarDeclaration > ( ) ) ;
45+ graphWalker . Walk ( rootArgsContext , graph , root . Node , rootArgsVisitor , cancellationToken ) ;
46+ args = rootArgsContext . Args ;
5047 }
5148 else
5249 {
53- isThreadSafe |= isThreadSafeRoot ;
54- }
50+ var ctx = new RootContext ( graph , root , varsMap , lines ) ;
51+ var rootVarInjection = rootBuilder ( ) . Build ( ctx ) ;
52+ var isThreadSafeRoot = ctx . LockIsInUse || graph . Source . Hints . IsThreadSafeEnabled && varsMap . IsThreadSafe ;
53+ if ( root . IsStatic )
54+ {
55+ if ( isThreadSafeRoot || overridesRegistry . GetOverrides ( root ) . Any ( ) )
56+ {
57+ // resolveLock local field
58+ var newLines = new Lines ( ) ;
59+ newLines . AppendLine ( new Line ( int . MinValue , "#if NET9_0_OR_GREATER" ) ) ;
60+ newLines . AppendLine ( $ "var { Names . PerResolveLockFieldName } = new { Names . LockTypeName } ();") ;
61+ newLines . AppendLine ( new Line ( int . MinValue , "#else" ) ) ;
62+ newLines . AppendLine ( $ "var { Names . PerResolveLockFieldName } = new { Names . ObjectTypeName } ();") ;
63+ newLines . AppendLine ( new Line ( int . MinValue , "#endif" ) ) ;
64+ newLines . AppendLines ( ctx . Lines ) ;
65+ lines = newLines ;
66+ ctx = ctx with { Lines = lines } ;
67+ }
68+ }
69+ else
70+ {
71+ isThreadSafe |= isThreadSafeRoot ;
72+ }
5573
56- lines . AppendLine ( $ "return { rootVarInjection . Var . CodeExpression } ;") ;
57- foreach ( var localFunction in varsMap . Vars . Select ( i => i . LocalFunction ) . Where ( i => i . Count > 0 ) )
58- {
59- lines . AppendLine ( ) ;
60- lines . AppendLines ( localFunction ) ;
74+ lines . AppendLine ( $ "return { rootVarInjection . Var . CodeExpression } ;") ;
75+ foreach ( var localFunction in varsMap . Vars . Select ( i => i . LocalFunction ) . Where ( i => i . Count > 0 ) )
76+ {
77+ lines . AppendLine ( ) ;
78+ lines . AppendLines ( localFunction ) ;
79+ }
80+
81+ processedRoot = processedRoot with { Lines = ctx . Lines } ;
82+ args = varsMap . Declarations . Where ( i => i . Node . Arg is not null ) ;
6183 }
6284
63- var args = varDeclarationTools . Sort ( varsMap . Declarations . Where ( i => i . Node . Arg is not null ) ) . ToList ( ) ;
64- var rootArgs = args . GetArgsOfKind ( ArgKind . Root ) ;
65- var processedRoot = root with
66- {
67- Lines = ctx . Lines ,
68- TypeDescription = typeResolver . Resolve ( graph . Source , root . Injection . Type ) ,
69- RootArgs = rootArgs . ToImmutableArray ( )
70- } ;
85+ var currentArgs = varDeclarationTools . Sort ( args ) . ToList ( ) ;
86+
87+ var currentClassArgs = currentArgs . GetArgsOfKind ( ArgKind . Composition )
88+ . Where ( arg => arg . Node . Arg is not { Source . IsSetupContext : true } )
89+ . Where ( arg => bindingsRegistry . IsRegistered ( graph . Source , arg . Node . BindingId ) ) ;
90+
91+ classArgs . AddRange ( currentClassArgs ) ;
92+
93+ var currentRootArgs = currentArgs . GetArgsOfKind ( ArgKind . Root ) . ToImmutableArray ( ) ;
7194
72- classArgs . AddRange ( args . GetArgsOfKind ( ArgKind . Composition )
73- . Where ( node => node . Node . Arg is not { Source . IsSetupContext : true } )
74- . Where ( node => bindingsRegistry . IsRegistered ( graph . Source , node . Node . BindingId ) ) ) ;
75- var typeDescription = typeResolver . Resolve ( graph . Source , processedRoot . Injection . Type ) ;
7695 var isMethod = processedRoot . Source . IsBuilder
7796 || ( processedRoot . Kind & RootKinds . Method ) == RootKinds . Method
78- || processedRoot . RootArgs . Length > 0
97+ || currentRootArgs . Length > 0
7998 || typeDescription . TypeArgs . Count > 0 ;
8099
81100 processedRoot = processedRoot with
82101 {
83- TypeDescription = typeDescription ,
102+ RootArgs = currentRootArgs . ToImmutableArray ( ) ,
84103 IsMethod = isMethod
104+ // , Kind = processedRoot.Kind & ~RootKinds.Light
85105 } ;
86106
87- if ( processedRoot . Kind . HasFlag ( RootKinds . Light ) && typeDescription . TypeArgs . Count > 0 )
88- {
89- processedRoot = processedRoot with { Kind = processedRoot . Kind & ~ RootKinds . Light } ;
90- }
91-
92107 roots . Add ( processedRoot ) ;
93108 }
94109
0 commit comments