@@ -21,11 +21,29 @@ public static class CqrsRouteMapper
21
21
22
22
private static readonly string [ ] GetAndHeadMethods = { "GET" , "HEAD" } ;
23
23
24
- private static readonly List < string > PostCommandPrefixes = new ( ) { "Create" , "Add" , "New" } ;
24
+ private static readonly List < string > PostCommandPrefixes = new ( )
25
+ {
26
+ "Create" ,
27
+ "Add" ,
28
+ "New"
29
+ } ;
25
30
26
- private static readonly List < string > PutCommandPrefixes = new ( ) { "Update" , "Modify" , "Replace" , "Alter" } ;
31
+ private static readonly List < string > PutCommandPrefixes = new ( )
32
+ {
33
+ "Update" ,
34
+ "Modify" ,
35
+ "Replace" ,
36
+ "Alter"
37
+ } ;
27
38
28
- private static readonly List < string > DeleteCommandPrefixes = new ( ) { "Delete" , "Remove" , "Clean" , "Clear" , "Purge" } ;
39
+ private static readonly List < string > DeleteCommandPrefixes = new ( )
40
+ {
41
+ "Delete" ,
42
+ "Remove" ,
43
+ "Clean" ,
44
+ "Clear" ,
45
+ "Purge"
46
+ } ;
29
47
30
48
/// <summary>
31
49
/// Map a query API, using GET method. <typeparamref name="T"/> would been constructed from route and query string.
@@ -96,14 +114,7 @@ public static IEndpointConventionBuilder MapQuery(
96
114
string nullRouteParameterPattern = "-" ,
97
115
bool enableHead = false )
98
116
{
99
- var isQuery = handler . Method . ReturnType . GetInterfaces ( ) . Where ( x => x . IsGenericType )
100
- . Any ( x => QueryTypes . Contains ( x . GetGenericTypeDefinition ( ) ) ) ;
101
- if ( isQuery == false )
102
- {
103
- throw new ArgumentException (
104
- "delegate does not return a query, please make sure it returns object that implement IQuery<> or IListQuery<> or interface that inherit from them" ) ;
105
- }
106
-
117
+ var returnType = EnsureReturnTypeIsQuery ( handler ) ;
107
118
if ( mapNullableRouteParameters is MapNullableRouteParameter . Disable )
108
119
{
109
120
return MapRoutes ( route ) ;
@@ -118,7 +129,7 @@ public static IEndpointConventionBuilder MapQuery(
118
129
119
130
var parsedRoute = RoutePatternFactory . Parse ( route ) ;
120
131
var context = new NullabilityInfoContext ( ) ;
121
- var nullableRouteProperties = handler . Method . ReturnType . GetProperties ( )
132
+ var nullableRouteProperties = returnType . GetProperties ( )
122
133
. Where (
123
134
p => p . GetMethod != null
124
135
&& p . SetMethod != null
@@ -209,8 +220,7 @@ public static IEndpointConventionBuilder MapCommand(
209
220
[ StringSyntax ( "Route" ) ] string route ,
210
221
Delegate handler )
211
222
{
212
- EnsureDelegateReturnTypeIsCommand ( handler ) ;
213
- var commandTypeName = handler . Method . ReturnType . Name ;
223
+ var commandTypeName = EnsureReturnTypeIsCommand ( handler ) . Name ;
214
224
if ( PostCommandPrefixes . Any ( x => commandTypeName . StartsWith ( x ) ) )
215
225
{
216
226
return app . MapPostCommand ( route , handler ) ;
@@ -255,7 +265,7 @@ public static IEndpointConventionBuilder MapPostCommand(
255
265
[ StringSyntax ( "Route" ) ] string route ,
256
266
Delegate handler )
257
267
{
258
- EnsureDelegateReturnTypeIsCommand ( handler ) ;
268
+ EnsureReturnTypeIsCommand ( handler ) ;
259
269
return app . MapPost ( route , handler ) . AddEndpointFilter < CommandEndpointHandler > ( ) ;
260
270
}
261
271
@@ -285,7 +295,7 @@ public static IEndpointConventionBuilder MapPutCommand(
285
295
[ StringSyntax ( "Route" ) ] string route ,
286
296
Delegate handler )
287
297
{
288
- EnsureDelegateReturnTypeIsCommand ( handler ) ;
298
+ EnsureReturnTypeIsCommand ( handler ) ;
289
299
return app . MapPut ( route , handler ) . AddEndpointFilter < CommandEndpointHandler > ( ) ;
290
300
}
291
301
@@ -315,7 +325,7 @@ public static IEndpointConventionBuilder MapDeleteCommand(
315
325
[ StringSyntax ( "Route" ) ] string route ,
316
326
Delegate handler )
317
327
{
318
- EnsureDelegateReturnTypeIsCommand ( handler ) ;
328
+ EnsureReturnTypeIsCommand ( handler ) ;
319
329
return app . MapDelete ( route , handler ) . AddEndpointFilter < CommandEndpointHandler > ( ) ;
320
330
}
321
331
@@ -385,15 +395,42 @@ public static IEndpointRouteBuilder StopMappingPrefixToDelete(this IEndpointRout
385
395
return app ;
386
396
}
387
397
388
- private static void EnsureDelegateReturnTypeIsCommand ( Delegate handler )
398
+ private static Type EnsureReturnTypeIsCommand ( Delegate handler )
389
399
{
390
- var isCommand = handler . Method . ReturnType . GetInterfaces ( ) . Where ( x => x . IsGenericType )
400
+ var returnType = handler . Method . ReturnType ;
401
+ if ( returnType . IsGenericType && returnType . GetGenericTypeDefinition ( ) == typeof ( Task < > ) )
402
+ {
403
+ returnType = returnType . GenericTypeArguments . First ( ) ;
404
+ }
405
+
406
+ var isCommand = returnType . GetInterfaces ( ) . Where ( x => x . IsGenericType )
391
407
. Any ( x => CommandTypes . Contains ( x . GetGenericTypeDefinition ( ) ) ) ;
392
408
if ( isCommand == false )
393
409
{
394
410
throw new ArgumentException (
395
411
"handler does not return command, check if delegate returns type that implements ICommand<> or ICommand<,>" ) ;
396
412
}
413
+
414
+ return returnType ;
415
+ }
416
+
417
+ private static Type EnsureReturnTypeIsQuery ( Delegate handler )
418
+ {
419
+ var returnType = handler . Method . ReturnType ;
420
+ if ( returnType . IsGenericType && returnType . GetGenericTypeDefinition ( ) == typeof ( Task < > ) )
421
+ {
422
+ returnType = returnType . GenericTypeArguments . First ( ) ;
423
+ }
424
+
425
+ var isCommand = returnType . GetInterfaces ( ) . Where ( x => x . IsGenericType )
426
+ . Any ( x => QueryTypes . Contains ( x . GetGenericTypeDefinition ( ) ) ) ;
427
+ if ( isCommand == false )
428
+ {
429
+ throw new ArgumentException (
430
+ "handler does not return query, check if delegate returns type that implements IQuery<>" ) ;
431
+ }
432
+
433
+ return returnType ;
397
434
}
398
435
399
436
private static List < T [ ] > GetNotEmptySubsets < T > ( ICollection < T > items )
0 commit comments