@@ -73,7 +73,7 @@ public class MessageBus : IMessageBus
73
73
readonly IServiceProvider services ;
74
74
readonly IServiceCollection ? collection ;
75
75
76
- // These executors are needed when the commadn types involved are not public.
76
+ // These executors are needed when the command types involved are not public.
77
77
// For the public cases, we just rely on the built-in dynamic dispatching
78
78
readonly ConcurrentDictionary < Type , VoidDispatcher > voidExecutors = new ( ) ;
79
79
readonly ConcurrentDictionary < Type , VoidAsyncDispatcher > voidAsyncExecutors = new ( ) ;
@@ -194,15 +194,18 @@ public void Execute(ICommand command, [CallerMemberName] string? callerName = de
194
194
195
195
try
196
196
{
197
- if ( type . IsPublic )
197
+ #if DYNAMIC_DISPATCH
198
+
199
+ if ( type . IsPublic || type . IsNestedPublic )
198
200
// For public types, we can use the faster dynamic dispatch approach
199
201
ExecuteCore ( ( dynamic ) command ) ;
200
202
else
203
+ #endif
201
204
voidExecutors . GetOrAdd ( type , type
202
- => ( VoidDispatcher ) Activator . CreateInstance (
203
- typeof ( VoidDispatcher < > ) . MakeGenericType ( type ) ,
204
- this ) ! )
205
- . Execute ( command ) ;
205
+ => ( VoidDispatcher ) Activator . CreateInstance (
206
+ typeof ( VoidDispatcher < > ) . MakeGenericType ( type ) ,
207
+ this ) ! )
208
+ . Execute ( command ) ;
206
209
}
207
210
catch ( Exception e )
208
211
{
@@ -221,9 +224,11 @@ public TResult Execute<TResult>(ICommand<TResult> command, [CallerMemberName] st
221
224
222
225
try
223
226
{
224
- if ( type . IsPublic )
227
+ #if DYNAMIC_DISPATCH
228
+ if ( type . IsPublic || type . IsNestedPublic )
225
229
// For public types, we can use the faster dynamic dispatch approach
226
230
return WithResult < TResult > ( ) . Execute ( ( dynamic ) command ) ;
231
+ #endif
227
232
228
233
return ( TResult ) resultExecutors . GetOrAdd ( type , type
229
234
=> ( ResultDispatcher ) Activator . CreateInstance (
@@ -248,10 +253,11 @@ public Task ExecuteAsync(IAsyncCommand command, CancellationToken cancellation =
248
253
249
254
try
250
255
{
251
- if ( type . IsPublic )
256
+ #if DYNAMIC_DISPATCH
257
+ if ( type . IsPublic || type . IsNestedPublic )
252
258
// For public types, we can use the faster dynamic dispatch approach
253
259
return ExecuteAsyncCore ( ( dynamic ) command , cancellation ) ;
254
-
260
+ #endif
255
261
return voidAsyncExecutors . GetOrAdd ( type , type
256
262
=> ( VoidAsyncDispatcher ) Activator . CreateInstance (
257
263
typeof ( VoidAsyncDispatcher < > ) . MakeGenericType ( type ) ,
@@ -275,10 +281,11 @@ public Task<TResult> ExecuteAsync<TResult>(IAsyncCommand<TResult> command, Cance
275
281
276
282
try
277
283
{
278
- if ( type . IsPublic )
284
+ #if DYNAMIC_DISPATCH
285
+ if ( type . IsPublic || type . IsNestedPublic )
279
286
// For public types, we can use the faster dynamic dispatch approach
280
287
return WithResult < TResult > ( ) . ExecuteAsync ( ( dynamic ) command , cancellation ) ;
281
-
288
+ #endif
282
289
return ( Task < TResult > ) resultAsyncExecutors . GetOrAdd ( type , type
283
290
=> ( ResultAsyncDispatcher ) Activator . CreateInstance (
284
291
typeof ( ResultAsyncDispatcher < , > ) . MakeGenericType ( type , typeof ( TResult ) ) ,
@@ -303,10 +310,11 @@ public IAsyncEnumerable<TResult> ExecuteStream<TResult>(IStreamCommand<TResult>
303
310
304
311
try
305
312
{
313
+ #if DYNAMIC_DISPATCH
306
314
if ( type . IsPublic || type . IsNestedPublic )
307
315
// For public types, we can use the faster dynamic dispatch approach
308
316
return WithResult < TResult > ( ) . ExecuteStream ( ( dynamic ) command , cancellation ) ;
309
-
317
+ #endif
310
318
return ( IAsyncEnumerable < TResult > ) resultAsyncExecutors . GetOrAdd ( type , type
311
319
=> ( ResultAsyncDispatcher ) Activator . CreateInstance (
312
320
typeof ( ResultStreamDispatcher < , > ) . MakeGenericType ( type , typeof ( TResult ) ) ,
@@ -734,7 +742,8 @@ abstract class VoidDispatcher
734
742
735
743
class VoidDispatcher < TCommand > ( MessageBus bus ) : VoidDispatcher where TCommand : ICommand
736
744
{
737
- public override void Execute ( IExecutable command ) => bus . ExecuteCore ( ( TCommand ) command ) ;
745
+ public override void Execute ( IExecutable command )
746
+ => bus . ExecuteCore ( ( TCommand ) command ) ;
738
747
}
739
748
740
749
abstract class ResultDispatcher
@@ -744,7 +753,8 @@ abstract class ResultDispatcher
744
753
745
754
class ResultDispatcher < TCommand , TResult > ( MessageBus bus ) : ResultDispatcher where TCommand : ICommand < TResult >
746
755
{
747
- public override object ? Execute ( IExecutable command ) => bus . ExecuteCore < TCommand , TResult > ( ( TCommand ) command ) ;
756
+ public override object ? Execute ( IExecutable command )
757
+ => bus . ExecuteCore < TCommand , TResult > ( ( TCommand ) command ) ;
748
758
}
749
759
750
760
abstract class VoidAsyncDispatcher
@@ -754,7 +764,8 @@ abstract class VoidAsyncDispatcher
754
764
755
765
class VoidAsyncDispatcher < TCommand > ( MessageBus bus ) : VoidAsyncDispatcher where TCommand : IAsyncCommand
756
766
{
757
- public override Task ExecuteAsync ( IExecutable command , CancellationToken cancellation ) => bus . ExecuteAsyncCore ( ( TCommand ) command , cancellation ) ;
767
+ public override Task ExecuteAsync ( IExecutable command , CancellationToken cancellation )
768
+ => bus . ExecuteAsyncCore ( ( TCommand ) command , cancellation ) ;
758
769
}
759
770
760
771
abstract class ResultAsyncDispatcher
0 commit comments