public ICqrsEventHandlerSelector ByAttribute(ServiceLifetime lifetime, params Assembly[] assemblies) { if (assemblies == null) { throw new ArgumentNullException(nameof(assemblies)); } if (assemblies.Length == 0) { throw new ArgumentException("No event handler assemblies were provided.", nameof(assemblies)); } _serviceCollection.Scan(scan => scan // Scan distinct assemblies. .FromAssemblies(assemblies.Distinct()) // Register classes that has a method marked with [EventHandler] .AddClasses(classes => classes.Where(type => EventHandlerAttributeMethod.IsFoundInType(type))) .AsSelf() .WithLifetime(lifetime)); _serviceCollection.AddSingleton <EventHandlerDelegateResolver>(serviceProvider => { var multiMessageHandlerRegistration = new MultiMessageHandlerRegistration(); multiMessageHandlerRegistration.RegisterEventHandlersByAttribute(assemblies, serviceProvider.GetRequiredService); return(new EventHandlerDelegateResolver(multiMessageHandlerRegistration.BuildMessageHandlerResolver())); }); return(this); }
public async Task ShouldRegisterAllMethodsMarkedWithEventHandlerAttribute() { var attributedHandler1 = new TestAttributedEventHandler(_outputHelper); var attributedHandler2 = new TestAttributedEventHandler(_outputHelper); var attributedHandler3 = new TestAttributedEventHandler(_outputHelper); var registration = new MultiMessageHandlerRegistration(); registration.RegisterEventHandlersByAttribute(() => attributedHandler1); registration.RegisterEventHandlersByAttribute(EventHandlerAttributeMethod.FromType <TestAttributedEventHandler>(() => attributedHandler2)); registration.RegisterEventHandlersByAttribute(EventHandlerAttributeMethod.FromType(typeof(TestAttributedEventHandler), () => attributedHandler3)); IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver(); MessageHandlerDelegate eventHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestEvent1)); // Get all methods marked with [EventHandler] and receiving TestEvent as parameter. int eventHandler1MethodCount = TestAttributedEventHandler.GetEventHandlerAttributeCountFor <TestEvent1>(); int eventHandler2MethodCount = TestAttributedEventHandler.GetEventHandlerAttributeCountFor <TestEvent1>(); int eventHandler3MethodCount = TestAttributedEventHandler.GetEventHandlerAttributeCountFor <TestEvent1>(); await eventHandlerDelegate.Invoke(new TestEvent1()); int totalEventHandlerMethodCount = eventHandler1MethodCount + eventHandler2MethodCount + eventHandler3MethodCount; int totalEventsHandledCount = attributedHandler1.HandledEvents.Count + attributedHandler2.HandledEvents.Count + attributedHandler3.HandledEvents.Count; totalEventsHandledCount.Should().Be(totalEventHandlerMethodCount); }
public ICqrsEventHandlerSelector 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 event handler assemblies were provided.", nameof(assemblies)); } // Get all types that has methods marked with [EventHandler] attribute from distinct assemblies. IEnumerable <Type> foundTypes = assemblies.Distinct() .SelectMany(assembly => assembly.GetTypes()) .Where(type => type.IsClass && !type.IsAbstract && EventHandlerAttributeMethod.IsFoundInType(type)) .ToArray(); if (!foundTypes.Any()) { return(this); } foreach (Type type in foundTypes) { // Register type as self. _container.Register(type, type, lifeStyle); } var multiMessageHandlerRegistration = new MultiMessageHandlerRegistration(); multiMessageHandlerRegistration.RegisterEventHandlersByAttribute(foundTypes, _container.GetInstance); // Register resolver. _container.Collections.AppendTo( typeof(EventHandlerDelegateResolver), Lifestyle.Singleton.CreateRegistration(() => new EventHandlerDelegateResolver(multiMessageHandlerRegistration.BuildMessageHandlerResolver()), _container ) ); return(this); }
public ICqrsEventHandlerSelector ByAttribute(Lifetime lifetime, params Assembly[] assemblies) { if (assemblies == null) { throw new System.ArgumentNullException(nameof(assemblies)); } if (assemblies.Length == 0) { throw new ArgumentException("No event handler assemblies were provided.", nameof(assemblies)); } Assembly[] distinctAssemblies = assemblies.Distinct().ToArray(); var attributeHandlerRegistration = _builder.RegisterAssemblyTypes(distinctAssemblies) .Where(type => type.IsClass && !type.IsAbstract && EventHandlerAttributeMethod.IsFoundInType(type)) .AsSelf(); // Update registration if lifetime is not PerDependency. switch (lifetime) { case Lifetime.PerLifetimeScope: { attributeHandlerRegistration.InstancePerLifetimeScope(); } break; case Lifetime.Singleton: { attributeHandlerRegistration.SingleInstance(); } break; } _builder.Register(context => { var c = context.Resolve <IComponentContext>(); var handlerRegistration = new MultiMessageHandlerRegistration(); handlerRegistration.RegisterEventHandlersByAttribute(distinctAssemblies, c.Resolve); return(new EventHandlerDelegateResolver(handlerRegistration.BuildMessageHandlerResolver())); }).AsSelf().SingleInstance(); return(this); }
/// <summary> /// Register methods of types from the list of assemblies that are marked with the [EventHandler] attribute as event handlers. /// <para>Supported signatures for methods marked with [EventHandler] are: (Methods can be named differently)</para> /// <para>void HandleEvent(TEvent event);</para> /// <para>Task HandleEventAsync(TEvent event);</para> /// <para>Task HandleEventAsync(TEvent event, CancellationToken cancellationToken);</para> /// </summary> /// <param name="registration">Message handler registration.</param> /// <param name="assemblies">Assemblies to scan for methods marked with the [EventHandler] attribute.</param> /// <param name="instanceFactory">Factory delegate that provides an instance of a type that has methods marked with [EventHandler] attribute.</param> public static void RegisterEventHandlersByAttribute(this MultiMessageHandlerRegistration registration, IEnumerable <Assembly> assemblies, Func <Type, object> instanceFactory) { RegisterEventHandlersByAttribute(registration, EventHandlerAttributeMethod.FromAssemblies(assemblies, instanceFactory)); }
/// <summary> /// Register methods of the specified type that are marked with the [EventHandler] attribute as event handlers. /// <para>Supported signatures for methods marked with [EventHandler] are: (Methods can be named differently)</para> /// <para>void HandleEvent(TEvent event);</para> /// <para>Task HandleEventAsync(TEvent event);</para> /// <para>Task HandleEventAsync(TEvent event, 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 RegisterEventHandlersByAttribute(this MultiMessageHandlerRegistration registration, Type type, Func <object> instanceFactory) { RegisterEventHandlersByAttribute(registration, EventHandlerAttributeMethod.FromType(type, instanceFactory)); }
/// <summary> /// Register methods marked with the [EventHandler] attribute as event handlers. /// <para>Supported signatures for methods marked with [EventHandler] are: (Methods can be named differently)</para> /// <para>void HandleEvent(TEvent event);</para> /// <para>Task HandleEventAsync(TEvent event);</para> /// <para>Task HandleEventAsync(TEvent event, CancellationToken cancellationToken);</para> /// </summary> /// <typeparam name="TAttributed">Type to search for methods marked with [EventHandler] attribute.</param> /// <remarks> /// This method will search for the methods marked with [EventHandler] in the type specified in type parameter. /// The type parameter should be the actual type that contains [EventHandler] 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 [EventHandler] attribute.</param> public static void RegisterEventHandlersByAttribute <TAttributed>(this MultiMessageHandlerRegistration registration, Func <TAttributed> attributedObjectFactory) where TAttributed : class { RegisterEventHandlersByAttribute(registration, EventHandlerAttributeMethod.FromType(attributedObjectFactory)); }