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(); } }
/// <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; }