public async Task ShouldRegisterAllCommandHandlerAttributeMethods() { var commandHandler = new TestAttributedCommandHandler(_outputHelper); // Get methods marked with [CommandHandler] attribute. IEnumerable <CommandHandlerAttributeMethod> methods = CommandHandlerAttributeMethod.FromType(() => commandHandler); var registration = new SingleMessageHandlerRegistration(); registration.RegisterCommandHandlersByAttribute(methods); IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver(); MessageHandlerDelegate commandHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestCommand)); commandHandlerDelegate.Should().NotBeNull(); // Delegate should invoke the actual command handler - TestAttributedCommandHandler. await commandHandlerDelegate.Invoke(new TestCommand()); commandHandler.HandledCommands.Should().HaveCount(1); commandHandler.HasHandledCommand <TestCommand>().Should().BeTrue(); }
/// <summary> /// Register methods of the specified type that are marked with the [CommandHandler] attribute as command handlers. /// <para>Supported signatures for methods marked with [CommandHandler] are: (Methods can be named differently)</para> /// <para>void HandleCommand(TCommand command);</para> /// <para>Task HandleCommandAsync(TCommand command);</para> /// <para>Task HandleCommandAsync(TCommand command, CancellationToken cancellationToken);</para> /// </summary> /// <param name="registration">Message handler registration.</param> /// <param name="type">Type to scan for methods marked with the [CommandHandler] attribute.</param> /// <param name="instanceFactory">Factory delegate that provides an instance of the specified type.</param> public static void RegisterCommandHandlersByAttribute(this SingleMessageHandlerRegistration registration, Type type, Func <object> instanceFactory) { RegisterCommandHandlersByAttribute(registration, CommandHandlerAttributeMethod.FromType(type, instanceFactory)); }
/// <summary> /// Register methods marked with the [CommandHandler] attribute as command handlers. /// <para>Supported signatures for methods marked with [CommandHandler] are: (Methods can be named differently)</para> /// <para>void HandleCommand(TCommand command);</para> /// <para>Task HandleCommandAsync(TCommand command);</para> /// <para>Task HandleCommandAsync(TCommand command, CancellationToken cancellationToken);</para> /// </summary> /// <typeparam name="TAttributed">Type to search for methods marked with [CommandHandler] attribute.</param> /// <remarks> /// This method will search for the methods marked with [CommandHandler] from the type specified in type parameter. /// The type parameter should be the actual type that contains [CommandHandler] methods. /// </remarks> /// <param name="registration">Message handler registration.</param> /// <param name="attributedObjectFactory">Factory delegate which provides an instance of a class that contains methods marked with [CommandHandler] attribute.</param> public static void RegisterCommandHandlersByAttribute <TAttributed>(this SingleMessageHandlerRegistration registration, Func <TAttributed> attributedObjectFactory) where TAttributed : class { RegisterCommandHandlersByAttribute(registration, CommandHandlerAttributeMethod.FromType <TAttributed>(attributedObjectFactory)); }