Beispiel #1
0
        /// <summary>
        /// Applies an event to the instance augmenting the current version
        /// for each event applied
        /// </summary>
        /// <param name="domainEvent">Event to apply</param>
        /// <param name="isNew">True if the event is new to the event stream; otherwise false</param>
        public void ApplyEvent(DomainEvent domainEvent, bool isNew = true)
        {
            Version++;

            if (isNew)
            {
                domainEvent.Sequence = Version;
                domainEvent.EventDate = DateTime.UtcNow;
            }

            // Call the apply method on the domain model instance
            if (!(domainEvent is DomainEntityEvent))
            {
                ApplyEventToSelf(domainEvent);
            }
            else
            {
                ApplyEventToEntities(domainEvent as DomainEntityEvent);
            }

            // Save the event for persistance if it's new
            if (isNew)
            {
                _appliedEvents.Add(domainEvent);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Publishes events to all event handlers
        /// </summary>
        /// <param name="domainEvent">Domain event instance</param>
        public void PublishEvent(DomainEvent domainEvent)
        {
            var domainEventType = domainEvent.GetType();

            var handlerType = typeof (IHandleDomainEvents<>);

            var handlers = _dependencyResolver.ResolveAll(handlerType.MakeGenericType(domainEventType));

            //TODO Order by some index
            foreach (var handler in handlers)
            {
                handler.AsDynamic().Handle(domainEvent);
            }
        }
Beispiel #3
0
        private void ApplyMethodWithCaching(object instanceToApply, DomainEvent eventToApply, Dictionary<string, MethodInfo> cache)
        {
            try
            {
                var eventType = eventToApply.GetType();
                var localKey = string.Format("{0},{1}", GetType().FullName, eventType);
                MethodInfo method;

                // Check of the handler (method info) for this event has been cached
                if (cache.ContainsKey(localKey))
                {
                    method = cache[localKey];
                }
                else
                {
                    // Get the convention-based handler
                    method = instanceToApply.GetAppliedEventMethodNamed(eventToApply.GetEventMethodName(), eventType);
                    cache.Add(localKey, method);
                }

                // Call the handler if it exists; otherwise dynamically call "Apply."
                if (method != null)
                {
                    method.Invoke(instanceToApply, new object[] { eventToApply });
                }
                else
                {
                    instanceToApply.AsDynamic().Apply(eventToApply);
                }
            }
            catch (Exception)
            {
                // The entity has no internal handler for the event.  No
                // need to trow an error.
            }
        }
Beispiel #4
0
 /// <summary>
 /// Applies a domain event to the current instance
 /// </summary>
 /// <param name="domainEvent">Event to apply</param>
 private void ApplyEventToSelf(DomainEvent domainEvent)
 {
     ApplyMethodWithCaching(this, domainEvent, CachedLocalMethods);
 }
Beispiel #5
0
 protected void ApplyEvent(DomainEvent domainEvent)
 {
     Parent.ApplyEvent(domainEvent);
 }
Beispiel #6
0
 /// <summary>
 /// Publishes a single event
 /// </summary>
 /// <param name="domainEvent">Event to publish</param>
 public void PublishEvent(DomainEvent domainEvent)
 {
     PublishEvents(new []{domainEvent});
 }