public void Threshold_should_not_hold_event_when_it_is_of_a_higher_type_when_exact_is_false()
        {
            Boolean handlerActionWasCalled = false;
            Action<ISourcedEvent> handlerAction = (e) => handlerActionWasCalled = true;

            var handler = new TypeThresholdedActionBasedDomainEventHandler(handlerAction, typeof(FooEvent), false);
            var handeled = handler.HandleEvent(new BarEvent());

            handeled.Should().Be(true);
            handlerActionWasCalled.Should().Be(true);
        }
Beispiel #2
0
        public void Threshold_should_hold_event_when_it_is_of_a_lower_type_when_exact_is_false()
        {
            Boolean         handlerActionWasCalled = false;
            Action <object> handlerAction          = (e) => handlerActionWasCalled = true;

            var handler  = new TypeThresholdedActionBasedDomainEventHandler(handlerAction, typeof(BarEvent), "", false);
            var handeled = handler.HandleEvent(new FooEvent());

            handeled.Should().Be(false);
            handlerActionWasCalled.Should().Be(false);
        }
        public void Threshold_should_hold_event_when_it_is_of_the_same_type_when_exact_is_true()
        {
            Boolean handlerActionWasCalled = false;
            Action<object> handlerAction = (e) => handlerActionWasCalled = true;

            var handler = new TypeThresholdedActionBasedDomainEventHandler(handlerAction, typeof(FooEvent), "", true);
            var handeled = handler.HandleEvent(new FooEvent());

            handeled.Should().Be(true);
            handlerActionWasCalled.Should().Be(true);
        }
Beispiel #4
0
        public void Threshold_should_hold_event_when_it_is_of_the_same_type_when_exact_is_true()
        {
            Boolean handlerActionWasCalled       = false;
            Action <ISourcedEvent> handlerAction = (e) => handlerActionWasCalled = true;

            var handler  = new TypeThresholdedActionBasedDomainEventHandler(handlerAction, typeof(FooEvent), true);
            var handeled = handler.HandleEvent(new FooEvent());

            handeled.Should().Be(true);
            handlerActionWasCalled.Should().Be(true);
        }
Beispiel #5
0
        public IEnumerable <ISourcedEventHandler> GetEventHandlers(object target)
        {
            Contract.Requires <ArgumentNullException>(target != null, "The target cannot be null.");
            Contract.Ensures(Contract.Result <IEnumerable <ISourcedEventHandler> >() != null, "The result should never be null.");

            var targetType = target.GetType();
            var handlers   = new List <ISourcedEventHandler>();

            Logger.DebugFormat("Trying to get all event handlers based by convention for {0}.", targetType);

            var methodsToMatch = targetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            var matchedMethods = from method in methodsToMatch
                                 let parameters = method.GetParameters()
                                                  let noEventHandlerAttributes =
                method.GetCustomAttributes(typeof(NoEventHandlerAttribute), true)
                where
                // Get only methods where the name matches.
                Regex.IsMatch(method.Name, _regexPattern, RegexOptions.CultureInvariant) &&
                // Get only methods that have 1 parameter.
                parameters.Length == 1 &&
                // Get only methods where the first parameter is an event.
                typeof(IEvent).IsAssignableFrom(parameters[0].ParameterType) &&
                // Get only methods that are not marked with the no event handler attribute.
                noEventHandlerAttributes.Length == 0
                select
                new { MethodInfo = method, FirstParameter = method.GetParameters()[0] };

            foreach (var method in matchedMethods)
            {
                var  methodCopy         = method.MethodInfo;
                Type firstParameterType = methodCopy.GetParameters().First().ParameterType;

                Action <IEvent> invokeAction = (e) => methodCopy.Invoke(target, new object[] { e });

                Logger.DebugFormat("Created event handler for method {0} based on convention.", methodCopy.Name);

                var handler = new TypeThresholdedActionBasedDomainEventHandler(invokeAction, firstParameterType, true);
                handlers.Add(handler);
            }

            return(handlers);
        }
        public IEnumerable<IDomainEventHandler> GetEventHandlersFromAggregateRoot(AggregateRoot aggregateRoot)
        {
            Contract.Requires<ArgumentNullException>(aggregateRoot != null, "The aggregateRoot cannot be null.");
            Contract.Ensures(Contract.Result<IEnumerable<IDomainEventHandler>>() != null, "The result should never be null.");

            var targetType = aggregateRoot.GetType();
            var handlers = new List<IDomainEventHandler>();

            Logger.DebugFormat("Trying to get all event handlers based by convention for {0}.", targetType);

            var methodsToMatch = targetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            var matchedMethods = from method in methodsToMatch
                                 let parameters = method.GetParameters()
                                 let noEventHandlerAttributes =
                                     method.GetCustomAttributes(typeof(NoEventHandlerAttribute), true)
                                 where
                                     // Get only methods where the name matches.
                                    Regex.IsMatch(method.Name, _regexPattern, RegexOptions.CultureInvariant) &&
                                     // Get only methods that have 1 parameter.
                                    parameters.Length == 1 &&
                                     // Get only methods where the first parameter is an event.
                                    typeof(DomainEvent).IsAssignableFrom(parameters[0].ParameterType) &&
                                     // Get only methods that are not marked with the no event handler attribute.
                                    noEventHandlerAttributes.Length == 0
                                 select
                                    new { MethodInfo = method, FirstParameter = method.GetParameters()[0] };

            foreach (var method in matchedMethods)
            {
                var methodCopy = method.MethodInfo;
                Type firstParameterType = methodCopy.GetParameters().First().ParameterType;

                Action<DomainEvent> invokeAction = (e) => methodCopy.Invoke(aggregateRoot, new object[] { e });

                Logger.DebugFormat("Created event handler for method {0} based on convention.", methodCopy.Name);

                var handler = new TypeThresholdedActionBasedDomainEventHandler(invokeAction, firstParameterType, true);
                handlers.Add(handler);
            }

            return handlers;
        }
        public IEnumerable<ISourcedEventHandler> GetEventHandlers(object target)
        {
            Contract.Requires<ArgumentNullException>(target != null, "The target cannot be null.");
            Contract.Ensures(Contract.Result<IEnumerable<ISourcedEventHandler>>() != null, "The result should never be null.");

            ICollection<ConventionMatchedMethod> matchedMethods = getConventionMatchedMethods(target);

            var handlers = new List<ISourcedEventHandler>(matchedMethods.Count);
            foreach (ConventionMatchedMethod method in matchedMethods)
            {
                MethodInfo methodCopy = method.MethodInfo;
                Type parameterType = method.EventType.ParameterType;

                Logger.DebugFormat("Created event handler for method {0} based on convention.", methodCopy.Name);
                Action<object> invokeAction = e => methodCopy.Invoke(target, new[] {e});
                var handler = new TypeThresholdedActionBasedDomainEventHandler(invokeAction, parameterType, methodCopy.Name, true);
                handlers.Add(handler);
            }
            return handlers;
        }