@@ -36,74 +36,13 @@ private static DiagnosticModel<MethodImplementationModel> FindServicesToRegister
3636 {
3737 typesFound = true ;
3838
39- var implementationTypeName = implementationType . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
40-
4139 if ( method . ReturnTypeIsCollection )
42- {
43- if ( attribute . CustomHandler == null )
44- {
45- // Case 1: no handler, return typeof(T) expressions
46- collectionItems . Add ( $ "typeof({ implementationTypeName } )") ;
47- }
48- else
49- {
50- // Case 2: handler maps T -> TResponse, generate handler invocation expressions
51- var arguments = string . Join ( ", " , method . Parameters . Select ( p => p . Name ) ) ;
52-
53- if ( attribute . CustomHandlerMethodTypeParametersCount > 1 && matchedTypes != null )
54- {
55- foreach ( var matchedType in matchedTypes )
56- {
57- var typeArguments = string . Join ( ", " , new [ ] { implementationTypeName }
58- . Concat ( matchedType . TypeArguments . Select ( a => a . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ) ) ) ;
59-
60- if ( attribute . CustomHandlerType == CustomHandlerType . Method )
61- collectionItems . Add ( $ "{ attribute . CustomHandler } <{ typeArguments } >({ arguments } )") ;
62- else
63- collectionItems . Add ( $ "{ implementationTypeName } .{ attribute . CustomHandler } ({ arguments } )") ;
64- }
65- }
66- else
67- {
68- if ( attribute . CustomHandlerType == CustomHandlerType . Method )
69- collectionItems . Add ( $ "{ attribute . CustomHandler } <{ implementationTypeName } >({ arguments } )") ;
70- else
71- collectionItems . Add ( $ "{ implementationTypeName } .{ attribute . CustomHandler } ({ arguments } )") ;
72- }
73- }
74- }
40+ AddCollectionItems ( implementationType , matchedTypes , attribute , method , collectionItems ) ;
7541 else if ( attribute . CustomHandler != null )
76- {
77- // If CustomHandler method has multiple type parameters, which are resolvable from the first one - we try to provide them.
78- // e.g. ApplyConfiguration<T, TEntity>(ModelBuilder modelBuilder) where T : IEntityTypeConfiguration<TEntity>
79- if ( attribute . CustomHandlerMethodTypeParametersCount > 1 && matchedTypes != null )
80- {
81- foreach ( var matchedType in matchedTypes )
82- {
83- EquatableArray < string > typeArguments =
84- [
85- implementationTypeName ,
86- .. matchedType . TypeArguments . Select ( a => a . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) )
87- ] ;
88-
89- customHandlers . Add ( new CustomHandlerModel (
90- attribute . CustomHandlerType . Value ,
91- attribute . CustomHandler ,
92- implementationTypeName ,
93- typeArguments ) ) ;
94- }
95- }
96- else
97- {
98- customHandlers . Add ( new CustomHandlerModel (
99- attribute . CustomHandlerType . Value ,
100- attribute . CustomHandler ,
101- implementationTypeName ,
102- [ implementationTypeName ] ) ) ;
103- }
104- }
42+ AddCustomHandlerItems ( implementationType , matchedTypes , attribute , customHandlers ) ;
10543 else
10644 {
45+ var implementationTypeName = implementationType . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
10746 var serviceTypes = ( attribute . AsSelf , attribute . AsImplementedInterfaces ) switch
10847 {
10948 ( true , true ) => [ implementationType , .. GetSuitableInterfaces ( implementationType ) ] ,
@@ -158,6 +97,81 @@ .. matchedType.TypeArguments.Select(a => a.ToDisplayString(SymbolDisplayFormat.F
15897 return new ( diagnostic , implementationModel ) ;
15998 }
16099
100+ private static void AddCollectionItems (
101+ INamedTypeSymbol implementationType ,
102+ IEnumerable < INamedTypeSymbol > ? matchedTypes ,
103+ AttributeModel attribute ,
104+ MethodModel method ,
105+ List < string > collectionItems )
106+ {
107+ var implementationTypeName = implementationType . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
108+
109+ if ( attribute . CustomHandler == null )
110+ {
111+ collectionItems . Add ( $ "typeof({ implementationTypeName } )") ;
112+ }
113+ else
114+ {
115+ var arguments = string . Join ( ", " , method . Parameters . Select ( p => p . Name ) ) ;
116+
117+ if ( attribute . CustomHandlerMethodTypeParametersCount > 1 && matchedTypes != null )
118+ {
119+ foreach ( var matchedType in matchedTypes )
120+ {
121+ var typeArguments = string . Join ( ", " , new [ ] { implementationTypeName }
122+ . Concat ( matchedType . TypeArguments . Select ( a => a . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ) ) ) ;
123+
124+ if ( attribute . CustomHandlerType == CustomHandlerType . Method )
125+ collectionItems . Add ( $ "{ attribute . CustomHandler } <{ typeArguments } >({ arguments } )") ;
126+ else
127+ collectionItems . Add ( $ "{ implementationTypeName } .{ attribute . CustomHandler } ({ arguments } )") ;
128+ }
129+ }
130+ else
131+ {
132+ if ( attribute . CustomHandlerType == CustomHandlerType . Method )
133+ collectionItems . Add ( $ "{ attribute . CustomHandler } <{ implementationTypeName } >({ arguments } )") ;
134+ else
135+ collectionItems . Add ( $ "{ implementationTypeName } .{ attribute . CustomHandler } ({ arguments } )") ;
136+ }
137+ }
138+ }
139+
140+ private static void AddCustomHandlerItems (
141+ INamedTypeSymbol implementationType ,
142+ IEnumerable < INamedTypeSymbol > ? matchedTypes ,
143+ AttributeModel attribute ,
144+ List < CustomHandlerModel > customHandlers )
145+ {
146+ var implementationTypeName = implementationType . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
147+
148+ if ( attribute . CustomHandlerMethodTypeParametersCount > 1 && matchedTypes != null )
149+ {
150+ foreach ( var matchedType in matchedTypes )
151+ {
152+ EquatableArray < string > typeArguments =
153+ [
154+ implementationTypeName ,
155+ .. matchedType . TypeArguments . Select ( a => a . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) )
156+ ] ;
157+
158+ customHandlers . Add ( new CustomHandlerModel (
159+ attribute . CustomHandlerType . Value ,
160+ attribute . CustomHandler ,
161+ implementationTypeName ,
162+ typeArguments ) ) ;
163+ }
164+ }
165+ else
166+ {
167+ customHandlers . Add ( new CustomHandlerModel (
168+ attribute . CustomHandlerType . Value ,
169+ attribute . CustomHandler ,
170+ implementationTypeName ,
171+ [ implementationTypeName ] ) ) ;
172+ }
173+ }
174+
161175 private static IEnumerable < INamedTypeSymbol > GetSuitableInterfaces ( ITypeSymbol type )
162176 {
163177 return type . AllInterfaces . Where ( x => ! ExcludedInterfaces . Contains ( x . ToDisplayString ( ) ) ) ;
0 commit comments