public static void CallEventHandler(Type aggregateRootType, MethodInfo eventHandler, object evnt, string propertyName, bool getWithLock)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            var eventType    = evnt.GetType();
            var propertyInfo = eventType.GetProperty(propertyName);

            if (propertyInfo == null)
            {
                throw new EventSourcingException(string.Format("Property '{0}' not exist in event type '{1}'", propertyName, eventType.FullName));
            }

            var aggregateRootId = propertyInfo.GetValue(evnt, null);

            if (aggregateRootId != null)
            {
                var logger        = DependencyResolver.Resolve <ILoggerFactory>().Create("EventSourcing.AggregateRootEventHandlerCaller");
                var aggregateRoot = AggregateRootLocator.GetAggregateRoot(aggregateRootType, aggregateRootId, getWithLock);
                if (aggregateRoot != null)
                {
                    if (logger.IsDebugEnabled)
                    {
                        logger.DebugFormat("Invoking AggregateRoot {0} EventHandler, event type:{1}.", aggregateRootType.Name, eventType.Name);
                    }
                    eventHandler.Invoke(aggregateRoot, new object[] { evnt });
                    if (logger.IsDebugEnabled)
                    {
                        logger.DebugFormat("Invoked AggregateRoot {0} EventHandler, event type:{1}.", aggregateRootType.Name, eventType.Name);
                    }
                }
                else
                {
                    logger.ErrorFormat(
                        "Could not find the aggregate root as a subscriber of event, Please verify whether it was deleted from the database. AggregateRoot Id:{0}, Event Type:{1}, AggregateRootId PropertyName:{2}",
                        aggregateRootId,
                        eventType.FullName,
                        propertyName
                        );
                }
            }
        }
Esempio n. 2
0
        private static AggregateRoot GetAggregateRootFromProperty(Type aggregateRootType, object sourceObject, string propertyName)
        {
            if (aggregateRootType == null || sourceObject == null || string.IsNullOrEmpty(propertyName))
            {
                return(null);
            }

            var propertyInfo = sourceObject.GetType().GetProperty(propertyName);

            if (propertyInfo == null)
            {
                throw new EventSourcingException(string.Format("Property '{0}' not exist in object type '{1}'", propertyName, sourceObject.GetType().FullName));
            }
            var aggregateRootId = propertyInfo.GetValue(sourceObject, null);

            if (aggregateRootId != null)
            {
                return(AggregateRootLocator.GetAggregateRoot(aggregateRootType, aggregateRootId));
            }

            return(null);
        }