Stove icon indicating copy to clipboard operation
Stove copied to clipboard

Domain event handlers are fired multiple times in case of registering multiple handlers for the same domain event.

Open CanerPatir opened this issue 6 years ago • 0 comments

test case like following

 public class EventHandlerTests  
    {
        [Fact]
        public void should_work()
        {
            var resolver = IocBuilder.New.UseAutofacContainerBuilder()
                .UseStoveWithNullables(typeof(FakeBootstrapper)).UseStoveEventBus()
                .RegisterServices(s =>
                {
                    s.RegisterType<FakeBootstrapper>();
                    s.RegisterType<FakeSendToImageProcessorEventHandler>();
                    s.RegisterType<FakeVisualMappingCompletedEventHandler>();
                })
                .CreateResolver();


            var eventBus = resolver.Resolve<IEventBus>();

            eventBus.Register(typeof(VisualMappingCompletedEvent), new IocHandlerFactory(resolver, typeof(FakeSendToImageProcessorEventHandler)));

            eventBus.Register(typeof(VisualMappingCompletedEvent), new IocHandlerFactory(resolver, typeof(FakeVisualMappingCompletedEventHandler)));

            eventBus.Publish<VisualMappingCompletedEvent>(new VisualMappingCompletedEvent(), new Headers());
        }
    }


    public class FakeSendToImageProcessorEventHandler : EventHandlerBase, IEventHandler<VisualMappingCompletedEvent>, ITransientDependency
    {
        static int a ;
        public void Handle(VisualMappingCompletedEvent @event, Headers headers)
        {
            a++;
        }
    }

    public class FakeVisualMappingCompletedEventHandler : EventHandlerBase, IEventHandler<VisualMappingCompletedEvent>, ITransientDependency
    {
        static int b;
        public void Handle(VisualMappingCompletedEvent @event, Headers headers)
        {
            b++;
        }
    }

    public class FakeBootstrapper : StoveBootstrapper
    {

    }

In this case you will see a = 2, b = 2

CanerPatir avatar Jun 28 '18 07:06 CanerPatir