1- using Cortex . Mediator . Commands ;
1+ using Cortex . Mediator . Commands ;
22using Cortex . Mediator . Queries ;
33using System ;
44using System . Collections . Generic ;
@@ -11,25 +11,64 @@ public class MediatorOptions
1111 internal List < Type > CommandBehaviors { get ; } = new ( ) ;
1212 internal List < Type > QueryBehaviors { get ; } = new ( ) ;
1313
14+ public bool OnlyPublicClasses { get ; set ; } = true ;
15+
16+
17+ /// <summary>
18+ /// Register a *closed* command pipeline behavior.
19+ /// </summary>
1420 public MediatorOptions AddCommandPipelineBehavior < TBehavior > ( )
15- where TBehavior : ICommandPipelineBehavior < ICommand > // Add constraint
21+ where TBehavior : class // Add constraint
1622 {
1723 var behaviorType = typeof ( TBehavior ) ;
24+
1825 if ( behaviorType . IsGenericTypeDefinition )
1926 {
2027 throw new ArgumentException ( "Open generic types must be registered using AddOpenCommandPipelineBehavior" ) ;
2128 }
29+
30+ var implementsInterface = behaviorType
31+ . GetInterfaces ( )
32+ . Any ( i => i . IsGenericType &&
33+ i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < , > ) ) ;
34+
35+ if ( ! implementsInterface )
36+ {
37+ throw new ArgumentException ( "Type must implement ICommandPipelineBehavior<,>" ) ;
38+ }
39+
2240 CommandBehaviors . Add ( behaviorType ) ;
2341 return this ;
2442 }
2543
44+ /// <summary>
45+ /// Register an *open generic* command pipeline behavior, e.g. typeof(LoggingCommandBehavior<,>).
46+ /// </summary>
2647 public MediatorOptions AddOpenCommandPipelineBehavior ( Type openGenericBehaviorType )
2748 {
2849 if ( ! openGenericBehaviorType . IsGenericTypeDefinition )
2950 {
3051 throw new ArgumentException ( "Type must be an open generic type definition" ) ;
3152 }
3253
54+ var implementsInterface = openGenericBehaviorType
55+ . GetInterfaces ( )
56+ . Any ( i => i . IsGenericType &&
57+ i . GetGenericTypeDefinition ( ) == typeof ( ICommandPipelineBehavior < , > ) ) ;
58+
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+ }
71+
3372 CommandBehaviors . Add ( openGenericBehaviorType ) ;
3473 return this ;
3574 }
0 commit comments