/// <summary>
        ///   Handles the event.
        /// </summary>
        /// <param name = "evnt">The event to handle.
        ///   <remarks>
        ///     This value should not be <c>null</c>.
        ///   </remarks>
        /// </param>
        /// <returns>
        ///   <c>true</c> when the event was handled; otherwise, <c>false</c>.
        ///   <remarks>
        ///     <c>false</c> does not mean that the handling failed, but that the
        ///     handler was not interested in handling this event.
        ///   </remarks>
        /// </returns>
        public bool HandleEvent(DomainEvent evnt)
        {
            Contract.Requires<ArgumentNullException>(evnt != null, "The evnt cannot be null.");

            var handled = false;

            if (ShouldHandleThisEvent(evnt))
            {
                _handler(evnt);
                handled = true;
            }

            return handled;
        }
Example #2
0
        public void It_should_call_the_converter_for_each_event()
        {
            var store = MockRepository.GenerateMock<IEventStore>();
            var bus = MockRepository.GenerateMock<IEventBus>();
            var converter = MockRepository.GenerateMock<IEventConverter<DomainEvent, DomainEvent>>();

            Func<DomainEvent, DomainEvent> returnFirstParam = (x) => x;
            converter.Stub(c => c.Convert(null)).IgnoreArguments().Do(returnFirstParam);

            var aggId = Guid.NewGuid();
            var eventsInTheStore = new DomainEvent[]
            {
                new FooEvent(Guid.NewGuid(), aggId, 1, DateTime.UtcNow),
                new BarEvent(Guid.NewGuid(), aggId, 2, DateTime.UtcNow)
            };
            store.Expect(s => s.GetAllEvents(aggId)).Return(eventsInTheStore);

            var repository = new DomainRepository(store, bus, null, converter);

            repository.GetById<MyAggregateRoot>(aggId);

            converter.AssertWasCalled(c => c.Convert(null),options => options.IgnoreArguments().Repeat.Twice());
        }
Example #3
0
        private void ApplyEvent(DomainEvent evnt, Boolean historical)
        {
            if(historical)
            {
                if(evnt.EventSequence != InitialVersion+1)
                {
                    var message = String.Format("Cannot apply event with sequence {0}. Since the initial version of the " +
                                                "aggregate root is {1}. Only an event with sequence number {2} can be applied.",
                                                evnt.EventSequence, InitialVersion, InitialVersion + 1);
                    throw new InvalidOperationException(message);
                }
            }
            else
            {
                if (evnt.AggregateRootId != Guid.Empty)
                {
                    var message = String.Format("The {0} event cannot be applied to aggregate root {1} with id {2} " +
                                                "since it was already owned by event aggregate root with id {3}.",
                                                evnt.GetType().FullName, this.GetType().FullName, Id, evnt.AggregateRootId);
                    throw new InvalidOperationException(message);
                }

                evnt.AggregateRootId = this.Id;
                evnt.EventSequence = Version + 1;
            }

            HandleEvent(evnt);

            if (!historical)
            {
                _uncommittedEvent.Enqueue(evnt);
                RegisterCurrentInstanceAsDirty();
            }
        }
Example #4
0
        protected virtual void HandleEvent(DomainEvent evnt)
        {
            Contract.Requires<ArgumentNullException>(evnt != null, "The evnt cannot be null.");
            Boolean handled = false;

            foreach (var handler in _eventHandlers)
            {
                handled |= handler.HandleEvent(evnt);
            }

            if (!handled)
                throw new EventNotHandledException(evnt);
        }
Example #5
0
 protected void ApplyEvent(DomainEvent evnt)
 {
     ApplyEvent(evnt, false);
 }
        /// <summary>
        ///   Determine whether the event should be handled or not.
        /// </summary>
        /// <param name = "evnt">The event.</param>
        /// <returns><c>true</c> when this event should be handled; otherwise, <c>false</c>.</returns>
        private bool ShouldHandleThisEvent(DomainEvent evnt)
        {
            Contract.Assume(evnt != null, "The evnt should not be null.");

            var shouldHandle = false;

            var evntType = evnt.GetType();

            // This is true when the eventTypeThreshold is
            // true if event type and the threshold type represent the same type, or if the theshold type is in the inheritance hierarchy
            // of the event type, or if the threshold type is an interface that event type implements.
            if (_eventTypeThreshold.IsAssignableFrom(evntType))
            {
                if (_exact)
                {
                    // Only handle the event when there is an exact match.
                    shouldHandle = (_eventTypeThreshold == evntType);
                }
                else
                {
                    // Handle the event, since it the threshold is assignable from the event type.
                    shouldHandle = true;
                }
            }

            return shouldHandle;
        }
 public new void ApplyEvent(DomainEvent e)
 {
     base.ApplyEvent(e);
 }
Example #8
0
        public bool HandleEvent(DomainEvent evnt)
        {
            Contract.Requires<ArgumentNullException>(evnt != null, "The evnt cannot be null.");

            return default(bool);
        }
Example #9
0
 private void CatchAllHandler(DomainEvent e)
 {
 }
 public void OnDomainEvent(DomainEvent e1, DomainEvent e2)
 {
 }
 public static void OnDomainEvent(DomainEvent e)
 {
 }