An internal event handler mapping strategy that creates event handlers based on mapping that is done by attributes. Use the EventHandlerAttribute to mark event handler methods as an event handler. You can only mark methods that following rules: The method should be an instance method (no static). It should accept 1 parameter. The parameter should be, or inherited from, the SourcedEvent class. The method should be marked with the EventHandlerAttribute. public class Foo : AggregateRootMappedWithAttributes { [EventHandler] private void onFooEvent(FooEvent eventToHandle) { // ... } }
Inheritance: ISourcedEventHandlerMappingStrategy
        public void It_should_map_the_mapped_events()
        {
            var aggregate = new GoodTarget();
            var mapping = new AttributeBasedDomainSourcedEventHandlerMappingStrategy();

            var handlers = mapping.GetEventHandlersFromAggregateRoot(aggregate);

            handlers.Count().Should().Be(5);
        }
        public void It_should_throw_an_exception_when_mapped_method_does_not_have_a_parameter()
        {
            var aggregate = new NoParameterMethodTarget();
            var mapping = new AttributeBasedDomainSourcedEventHandlerMappingStrategy();

            Action act = () => mapping.GetEventHandlersFromAggregateRoot(aggregate);

            act.ShouldThrow<InvalidEventHandlerMappingException>();
        }
        public void It_should_create_the_correct_event_handlers()
        {
            var aggregate = new GoodTarget();
            var mapping = new AttributeBasedDomainSourcedEventHandlerMappingStrategy();

            var handlers = mapping.GetEventHandlersFromAggregateRoot(aggregate);

            foreach(var handler in handlers)
            {
                handler.HandleEvent(new GoodTarget.PublicEvent());
                handler.HandleEvent(new GoodTarget.ProtectedEvent());
                handler.HandleEvent(new GoodTarget.InternalEvent());
                handler.HandleEvent(new GoodTarget.PrivateEvent());
            }

            aggregate.PublicEventHandlerInvokeCount.Should().Be(1);
            aggregate.ProtectedEventHandlerInvokeCount.Should().Be(1);
            aggregate.InternalEventHandlerInvokeCount.Should().Be(1);
            aggregate.PrivateEventHandlerInvokeCount.Should().Be(1);
            aggregate.CatchAllEventHandlerInvokeCount.Should().Be(4);
        }
        public void It_should_throw_an_exception_when_mapped_method_is_static()
        {
            var aggregate = new IlligalStaticMethodTarget();
            var mapping = new AttributeBasedDomainSourcedEventHandlerMappingStrategy();

            Action act = () => mapping.GetEventHandlersFromAggregateRoot(aggregate);

            act.ShouldThrow<InvalidEventHandlerMappingException>();
        }