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