Repository navigation
Support Handler method from other assembly for ScanForTypes attribute #60
Copy link
Copy link
Closed
Description
Recent addition of ScanForTypes attribute is really nice improvement!
I was wondering whether you're willing to support specyfing methods for Handler property that are implemented in other assembly?
This might be usefull for sharing some common registration methods and avoid duplication.
Sample usecase with modular application where each module will register it's own handlers and decorators:
- First Module
[ScanForTypes(
AssignableTo = typeof(ICommandHandler<>),
Handler = nameof(Extensions.DecorateCommandHandler)
)]
[ScanForTypes(
AssignableTo = typeof(ICommandHandler<,>),
Handler = nameof(Extensions.DecorateCommandHandlerWithResponse)
)]
[ScanForTypes(
AssignableTo = typeof(IEventHandler<>),
Handler = nameof(Extensions.DecorateIntegrationEventHandler)
)]
private static partial IServiceCollection AddTransactionalProcessing(
this IServiceCollection services
);- Second Module
[ScanForTypes(
AssignableTo = typeof(ICommandHandler<>),
Handler = nameof(Extensions.DecorateCommandHandler)
)]
[ScanForTypes(
AssignableTo = typeof(ICommandHandler<,>),
Handler = nameof(Extensions.DecorateCommandHandlerWithResponse)
)]
[ScanForTypes(
AssignableTo = typeof(IEventHandler<>),
Handler = nameof(Extensions.DecorateIntegrationEventHandler)
)]
private static partial IServiceCollection AddTransactionalProcessing(
this IServiceCollection services
);- Common Module (referenced by First and Second one)
public static class Extensions
{
public static void DecorateCommandHandler<THandler, TRequest>(IServiceCollection services)
where THandler : class, ICommandHandler<TRequest>
where TRequest : class, ICommand =>
services.Decorate<IRequestHandler<TRequest>, CommandTransactionDecorator<TRequest>>();
public static void DecorateCommandHandlerWithResponse<THandler, TRequest, TResult>(
IServiceCollection services
)
where THandler : class, ICommandHandler<TRequest, TResult>
where TRequest : class, ICommand<TResult> =>
services.Decorate<
IRequestHandler<TRequest, TResult>,
CommandWithResponseTransactionDecorator<TRequest, TResult>
>();
public static void DecorateIntegrationEventHandler<THandler, TEvent>(
IServiceCollection services
)
where THandler : class, IEventHandler<TEvent>
where TEvent : BaseIntegrationEvent =>
services.Decorate<
INotificationHandler<TEvent>,
IntegrationEventTransactionDecorator<TEvent>
>();
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
Dreamescaper commentedon Apr 24, 2026
Currently it is possible either with methods proxing to shared ones:
or via HandlerTemplate:
Although - referencing static method from another type might be a valuable addition. It involves analyzing an expression inside of
nameof- that contradicts C# semantics a bit (from C# perspectivenameof(Extensions.DecorateCommandHandler)and"DecorateCommandHandler"are the same thing). Which is probably not the end of the world.sebastianstudniczek commentedon Apr 24, 2026
Maybe it would be easier to add additional property
HandlerTypeor something similar?Dreamescaper commentedon Apr 24, 2026
I'd prefer not to bload API surface over this, supporting it via nameof should be fine.