@@ -9,6 +9,7 @@ namespace Cortex.Mediator.DependencyInjection
99 public class MediatorOptions
1010 {
1111 internal List < Type > CommandBehaviors { get ; } = new ( ) ;
12+ internal List < Type > VoidCommandBehaviors { get ; } = new ( ) ;
1213 internal List < Type > QueryBehaviors { get ; } = new ( ) ;
1314
1415 public bool OnlyPublicClasses { get ; set ; } = true ;
@@ -23,21 +24,25 @@ public MediatorOptions AddCommandPipelineBehavior<TBehavior>()
2324 var behaviorType = typeof ( TBehavior ) ;
2425
2526 if ( behaviorType . IsGenericTypeDefinition )
26- {
2727 throw new ArgumentException ( "Open generic types must be registered using AddOpenCommandPipelineBehavior" ) ;
28- }
2928
30- var implementsInterface = behaviorType
31- . GetInterfaces ( )
32- . Any ( i => i . IsGenericType &&
33- i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < , > ) ) ;
29+ var implementsReturning =
30+ behaviorType . GetInterfaces ( ) . Any ( i => i . IsGenericType &&
31+ i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < , > ) ) ;
3432
35- if ( ! implementsInterface )
36- {
37- throw new ArgumentException ( "Type must implement ICommandPipelineBehavior<,>" ) ;
38- }
33+ var implementsNonReturning =
34+ behaviorType . GetInterfaces ( ) . Any ( i => i . IsGenericType &&
35+ i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < > ) ) ;
36+
37+ if ( ! implementsReturning && ! implementsNonReturning )
38+ throw new ArgumentException ( "Type must implement ICommandPipelineBehavior<,> or ICommandPipelineBehavior<>" ) ;
39+
40+ if ( implementsReturning )
41+ CommandBehaviors . Add ( behaviorType ) ;
42+
43+ if ( implementsNonReturning )
44+ VoidCommandBehaviors . Add ( behaviorType ) ;
3945
40- CommandBehaviors . Add ( behaviorType ) ;
4146 return this ;
4247 }
4348
@@ -47,29 +52,25 @@ public MediatorOptions AddCommandPipelineBehavior<TBehavior>()
4752 public MediatorOptions AddOpenCommandPipelineBehavior ( Type openGenericBehaviorType )
4853 {
4954 if ( ! openGenericBehaviorType . IsGenericTypeDefinition )
50- {
5155 throw new ArgumentException ( "Type must be an open generic type definition" ) ;
52- }
5356
54- var implementsInterface = openGenericBehaviorType
55- . GetInterfaces ( )
56- . Any ( i => i . IsGenericType &&
57- i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < , > ) ) ;
57+ var implementsReturning =
58+ openGenericBehaviorType . GetInterfaces ( ) . Any ( i => i . IsGenericType &&
59+ i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < , > ) ) ;
5860
59- // For open generics, interface might not appear in GetInterfaces() yet; check by definition instead.
60- if ( ! implementsInterface &&
61- ! ( openGenericBehaviorType . IsGenericTypeDefinition &&
62- openGenericBehaviorType . GetGenericTypeDefinition ( ) == openGenericBehaviorType ) )
63- {
64- // Fall back to checking generic arguments count to give a clear error
65- var ok = openGenericBehaviorType . GetGenericArguments ( ) . Length == 2 ;
66- if ( ! ok )
67- {
68- throw new ArgumentException ( "Type must implement ICommandPipelineBehavior<,>" ) ;
69- }
70- }
61+ var implementsNonReturning =
62+ openGenericBehaviorType . GetInterfaces ( ) . Any ( i => i . IsGenericType &&
63+ i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < > ) ) ;
64+
65+ if ( ! implementsReturning && ! implementsNonReturning )
66+ throw new ArgumentException ( "Type must implement ICommandPipelineBehavior<,> or ICommandPipelineBehavior<>" ) ;
67+
68+ if ( implementsReturning )
69+ CommandBehaviors . Add ( openGenericBehaviorType ) ;
70+
71+ if ( implementsNonReturning )
72+ VoidCommandBehaviors . Add ( openGenericBehaviorType ) ;
7173
72- CommandBehaviors . Add ( openGenericBehaviorType ) ;
7374 return this ;
7475 }
7576
0 commit comments