-
Notifications
You must be signed in to change notification settings - Fork 9
Labels
enhancementNew feature or requestNew feature or requestfeatureThis label is in use for minor version incrementsThis label is in use for minor version increments
Milestone
Description
Title: Add source generator to eliminate reflection-based dispatch and enable NativeAOT
Labels: enhancement, mediator
Body:
Problem
Mediator.cs uses runtime reflection with ConcurrentDictionary<Type, MethodInfo> caches to dispatch commands, queries, and stream queries when the concrete type is only known at runtime (the single-generic-parameter overloads like SendCommandAsync<TResult>(ICommand<TResult>)). This has several drawbacks:
- Cold-start overhead from reflection and
MakeGenericMethodon first call per type - No NativeAOT / trimming support -- the reflection patterns are not trim-safe
- No compile-time validation -- a missing handler registration only fails at runtime
- Allocation pressure from
new object[] { command, cancellationToken }on every reflected dispatch
Proposed Solution
Create a Cortex.Mediator.SourceGenerator package that uses Roslyn incremental source generators to:
- Scan the compilation for all
ICommand<T>,IQuery<T>,IStreamQuery<T>, andINotificationimplementations - Generate a
GeneratedMediatorclass with strongly-typed dispatch (no reflection) - Generate DI registration code (
AddCortexMediator()) that registers all handlers without Scrutor - Emit build warnings/errors for commands/queries without matching handlers
- Support
[assembly: MediatorAssembly]or similar attribute to control scanning scope
Benefits
- Zero reflection at runtime
- Full NativeAOT and trimming compatibility
- Missing handler = compile error
- Removes Scrutor dependency when using source gen
- Aligns with .NET ecosystem direction (similar approach to
Mediatorby martinothamar)
Scope
- New project:
Cortex.Mediator.SourceGenerator - The existing reflection-based
Mediatorclass should remain as a fallback for dynamic scenarios - Users opt in by adding the source generator package
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestfeatureThis label is in use for minor version incrementsThis label is in use for minor version increments