Skip to content

Support Handler method from other assembly for ScanForTypes attribute #60

@sebastianstudniczek

Description

@sebastianstudniczek

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>
        >();
}

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions