public ICqrsCommandHandlerSelector ByAttribute(ServiceLifetime lifetime, params Assembly[] assemblies) { if (assemblies == null) { throw new ArgumentNullException(nameof(assemblies)); } if (assemblies.Length == 0) { throw new ArgumentException("No command handler assemblies were provided.", nameof(assemblies)); } _serviceCollection.Scan(scan => scan // Scan distinct assemblies. .FromAssemblies(assemblies.Distinct()) // Register classes that has a method marked with [CommandHandler] .AddClasses(classes => classes.Where(type => CommandHandlerAttributeMethod.IsFoundInType(type))) .AsSelf() .WithLifetime(lifetime)); _serviceCollection.AddSingleton <CommandHandlerDelegateResolver>(serviceProvider => { var singleMessageHandlerRegistration = new SingleMessageHandlerRegistration(); singleMessageHandlerRegistration.RegisterCommandHandlersByAttribute(assemblies, serviceProvider.GetRequiredService); return(new CommandHandlerDelegateResolver(singleMessageHandlerRegistration.BuildMessageHandlerResolver())); }); return(this); }
public ICqrsCommandHandlerSelector ByAttribute(Lifestyle lifeStyle, params Assembly[] assemblies) { if (lifeStyle == null) { throw new ArgumentNullException(nameof(lifeStyle)); } if (assemblies == null) { throw new ArgumentNullException(nameof(assemblies)); } if (assemblies.Length == 0) { throw new ArgumentException("No command handler assemblies were provided.", nameof(assemblies)); } // Get all types that has methods marked with [CommandHandler] attribute from distinct assemblies. IEnumerable <Type> foundTypes = assemblies.Distinct() .SelectMany(assembly => assembly.GetTypes()) .Where(type => type.IsClass && !type.IsAbstract && CommandHandlerAttributeMethod.IsFoundInType(type)) .ToArray(); if (!foundTypes.Any()) { return(this); } foreach (Type type in foundTypes) { // Register type as self. _container.Register(type, type, lifeStyle); } var singleMessageHandlerRegistration = new SingleMessageHandlerRegistration(); singleMessageHandlerRegistration.RegisterCommandHandlersByAttribute(foundTypes, _container.GetInstance); // Register resolver. _container.Collections.AppendTo( typeof(CommandHandlerDelegateResolver), Lifestyle.Singleton.CreateRegistration(() => new CommandHandlerDelegateResolver(singleMessageHandlerRegistration.BuildMessageHandlerResolver()), _container ) ); return(this); }
public ICqrsCommandHandlerSelector ByAttribute(Lifetime lifetime, params Assembly[] assemblies) { if (assemblies == null) { throw new ArgumentNullException(nameof(assemblies)); } if (assemblies.Length == 0) { throw new ArgumentException("No command handler assemblies were provided.", nameof(assemblies)); } Assembly[] distinctAssemblies = assemblies.Distinct().ToArray(); var handlerRegistration = _builder.RegisterAssemblyTypes(distinctAssemblies) .Where(type => type.IsClass && !type.IsAbstract && CommandHandlerAttributeMethod.IsFoundInType(type)) .AsSelf(); // Update registration if lifetime is not PerDependency. switch (lifetime) { case Lifetime.PerLifetimeScope: { handlerRegistration.InstancePerLifetimeScope(); } break; case Lifetime.Singleton: { handlerRegistration.SingleInstance(); } break; } _builder.Register(context => { var c = context.Resolve <IComponentContext>(); var singleMessageHandlerRegistration = new SingleMessageHandlerRegistration(); singleMessageHandlerRegistration.RegisterCommandHandlersByAttribute(distinctAssemblies, c.Resolve); return(new CommandHandlerDelegateResolver(singleMessageHandlerRegistration.BuildMessageHandlerResolver())); }).AsSelf().SingleInstance(); return(this); }
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 types from the assembly 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="assembly">Assembly to scan for methods marked with the [CommandHandler] attribute.</param> /// <param name="instanceFactory">Factory delegate that provides an instance of a type that has methods marked with [CommandHandler] attribute.</param> public static void RegisterCommandHandlersByAttribute(this SingleMessageHandlerRegistration registration, Assembly assembly, Func <Type, object> instanceFactory) { RegisterCommandHandlersByAttribute(registration, CommandHandlerAttributeMethod.FromAssembly(assembly, instanceFactory)); }
/// <summary> /// Register methods of types 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="types">Types to scan for methods marked with the [CommandHandler] attribute.</param> /// <param name="instanceFactory">Factory delegate that provides an instance of a given type.</param> public static void RegisterCommandHandlersByAttribute(this SingleMessageHandlerRegistration registration, IEnumerable <Type> types, Func <Type, object> instanceFactory) { RegisterCommandHandlersByAttribute(registration, CommandHandlerAttributeMethod.FromTypes(types, instanceFactory)); }
/// <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)); }