Beispiel #1
0
        /// <summary>
        /// Discover all event handlers associated with any locatable class marked with <see cref="EventHandlerAttribute"/>.
        /// </summary>
        /// <param name="sagaStore">The saga store to pass on to any <see cref="SagaEventHandler"/> instances.</param>
        /// <param name="typeLocator">The type locator use to retrieve all known classes marked with <see cref="EventHandlerAttribute"/>.</param>
        /// <param name="serviceProvider">The service locator used to retrieve singleton event handler dependencies.</param>
        /// <param name="commandPublisher">The command publisher used to publish saga commands.</param>
        private static Dictionary <Type, EventHandler[]> DiscoverEventHandlers(ILocateTypes typeLocator, IServiceProvider serviceProvider, IStoreSagas sagaStore, Lazy <IPublishCommands> commandPublisher)
        {
            var knownEvents   = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.DerivesFrom(typeof(Event)));
            var knownHandlers = DiscoverHandleMethods(typeLocator, serviceProvider, sagaStore, commandPublisher);
            var result        = new Dictionary <Type, EventHandler[]>();
            var logMessage    = new StringBuilder();

            logMessage.AppendLine("Discovered event handlers:");
            foreach (var eventType in knownEvents.OrderBy(type => type.FullName))
            {
                var eventHandlers = eventType.GetTypeHierarchy().Reverse()
                                    .Where(knownHandlers.ContainsKey)
                                    .SelectMany(type => knownHandlers[type])
                                    .OrderBy(handler => handler is SagaEventHandler)
                                    .ThenBy(handler => handler.HandlerType.AssemblyQualifiedName)
                                    .ToArray();

                logMessage.Append("    ");
                logMessage.Append(eventType);
                logMessage.AppendLine();

                foreach (var eventHandler in eventHandlers)
                {
                    logMessage.Append("        ");
                    logMessage.Append(eventHandler.HandlerType);
                    logMessage.AppendLine();
                }

                result.Add(eventType, eventHandlers);
            }

            Log.Debug(logMessage.ToString);

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Discover all event handlers methods associated with any locatable class marked with with <see cref="EventHandlerAttribute"/>.
        /// </summary>
        /// <param name="sagaStore">The saga store to pass on to any <see cref="SagaEventHandler"/> instances.</param>
        /// <param name="typeLocator">The type locator use to retrieve all known classes marked with <see cref="EventHandlerAttribute"/>.</param>
        /// <param name="serviceProvider">The service locator used to retrieve singleton event handler dependencies.</param>
        /// <param name="commandPublisher">The command publisher used to publish saga commands.</param>
        private static Dictionary <Type, List <EventHandler> > DiscoverHandleMethods(ILocateTypes typeLocator, IServiceProvider serviceProvider, IStoreSagas sagaStore, Lazy <IPublishCommands> commandPublisher)
        {
            var handlerTypes       = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.GetCustomAttribute <EventHandlerAttribute>() != null);
            var knownEventHandlers = new Dictionary <Type, List <EventHandler> >();

            foreach (var handlerType in handlerTypes)
            {
                var handleMethods = GetHandleMethods(handlerType, serviceProvider);
                var sagaMetadata  = typeof(Saga).IsAssignableFrom(handlerType) ? GetSagaMetadata(handlerType, handleMethods) : null;

                foreach (var handleMethod in handleMethods)
                {
                    List <EventHandler> eventHandlers;
                    Type eventType = handleMethod.Key;
                    if (!knownEventHandlers.TryGetValue(eventType, out eventHandlers))
                    {
                        knownEventHandlers.Add(eventType, eventHandlers = new List <EventHandler>());
                    }

                    var eventHandler = new EventHandler(handlerType, eventType, handleMethod.Value, GetHandlerFactory(handlerType, serviceProvider));
                    if (sagaMetadata != null)
                    {
                        eventHandler = eventType == typeof(Timeout) ? new SagaTimeoutHandler(eventHandler, sagaMetadata, sagaStore, commandPublisher) : new SagaEventHandler(eventHandler, sagaMetadata, sagaStore, commandPublisher);
                    }

                    eventHandlers.Add(eventHandler);
                }
            }

            return(knownEventHandlers);
        }
        /// <summary>
        /// Discover all known <see cref="Aggregate"/> implementations known to the specified <paramref name="typeLocator"/>.
        /// </summary>
        /// <param name="typeLocator">The type locator used to retrieve all <see cref="Aggregate"/> type information.</param>
        private static IDictionary<Type, ApplyMethodCollection> DiscoverAggregates(ILocateTypes typeLocator)
        {
            var aggregateTypes = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.DerivesFrom(typeof(Aggregate)));
            var result = new Dictionary<Type, ApplyMethodCollection>();

            foreach (var aggregateType in aggregateTypes)
                result.Add(aggregateType, DiscoverApplyMethods(aggregateType));

            return result;
        }
        /// <summary>
        /// Discover all known <see cref="Aggregate"/> implementations known to the specified <paramref name="typeLocator"/>.
        /// </summary>
        /// <param name="typeLocator">The type locator used to retrieve all <see cref="Aggregate"/> type information.</param>
        private static IDictionary <Type, ApplyMethodCollection> DiscoverAggregates(ILocateTypes typeLocator)
        {
            var aggregateTypes = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.DerivesFrom(typeof(Aggregate)));
            var result         = new Dictionary <Type, ApplyMethodCollection>();

            foreach (var aggregateType in aggregateTypes)
            {
                result.Add(aggregateType, DiscoverApplyMethods(aggregateType));
            }

            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Discover all command handlers methods associated with any locatable <see cref="Aggregate"/> type.
        /// </summary>
        /// <param name="typeLocator">The type locator use to retrieve all known <see cref="Aggregate"/> types.</param>
        /// <param name="serviceProvider">The service locator used to retrieve singleton command handler dependencies.</param>
        private static Dictionary <Type, HandleMethodCollection> DiscoverHandleMethods(ILocateTypes typeLocator, IServiceProvider serviceProvider)
        {
            var aggregateTypes       = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.DerivesFrom(typeof(Aggregate)));
            var knownCommandHandlers = new Dictionary <Type, HandleMethodCollection>();

            foreach (var aggregateType in aggregateTypes)
            {
                var handleMethods = GetHandleMethods(aggregateType, serviceProvider);

                knownCommandHandlers.Add(aggregateType, handleMethods);
            }

            return(knownCommandHandlers);
        }
Beispiel #6
0
        /// <summary>
        /// Generate unique saga type identifiers for all locatable <see cref="Saga"/> types.
        /// </summary>
        /// <param name="typeLocator">The type locator use to retrieve all known <see cref="Saga"/> types.</param>
        private static Dictionary <Type, Guid> GetKnownSagas(ILocateTypes typeLocator)
        {
            var knownSagas = typeLocator.GetTypes(type => type.IsClass && !type.IsAbstract && type.DerivesFrom(typeof(Saga))).ToDictionary(type => type, HashType);
            var logMessage = new StringBuilder();

            logMessage.AppendLine("Discovered sagas:");
            foreach (var saga in knownSagas)
            {
                logMessage.Append("    ");
                logMessage.AppendFormat("{0} - {1}", saga.Key, saga.Value);
                logMessage.AppendLine();
            }

            Log.Debug(logMessage.ToString);

            return(knownSagas);
        }
        /// <summary>
        /// Discover all event handlers associated with any locatable class marked with <see cref="EventHandlerAttribute"/>.
        /// </summary>
        /// <param name="sagaStore">The saga store to pass on to any <see cref="SagaEventHandler"/> instances.</param>
        /// <param name="typeLocator">The type locator use to retrieve all known classes marked with <see cref="EventHandlerAttribute"/>.</param>
        /// <param name="serviceProvider">The service locator used to retrieve singleton event handler dependencies.</param>
        /// <param name="commandPublisher">The command publisher used to publish saga commands.</param>
        private static Dictionary<Type, EventHandler[]> DiscoverEventHandlers(ILocateTypes typeLocator, IServiceProvider serviceProvider, IStoreSagas sagaStore, Lazy<IPublishCommands> commandPublisher)
        {
            var knownEvents = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.DerivesFrom(typeof(Event)));
            var knownHandlers = DiscoverHandleMethods(typeLocator, serviceProvider, sagaStore, commandPublisher);
            var result = new Dictionary<Type, EventHandler[]>();
            var logMessage = new StringBuilder();

            logMessage.AppendLine("Discovered event handlers:");
            foreach (var eventType in knownEvents.OrderBy(type => type.FullName))
            {
                var eventHandlers = eventType.GetTypeHierarchy().Reverse()
                                             .Where(knownHandlers.ContainsKey)
                                             .SelectMany(type => knownHandlers[type])
                                             .OrderBy(handler => handler is SagaEventHandler)
                                             .ThenBy(handler => handler.HandlerType.AssemblyQualifiedName)
                                             .ToArray();

                logMessage.Append("    ");
                logMessage.Append(eventType);
                logMessage.AppendLine();

                foreach (var eventHandler in eventHandlers)
                {
                    logMessage.Append("        ");
                    logMessage.Append(eventHandler.HandlerType);
                    logMessage.AppendLine();
                }

                result.Add(eventType, eventHandlers);
            }

            Log.Debug(logMessage.ToString);

            return result;
        }
        /// <summary>
        /// Discover all event handlers methods associated with any locatable class marked with with <see cref="EventHandlerAttribute"/>.
        /// </summary>
        /// <param name="sagaStore">The saga store to pass on to any <see cref="SagaEventHandler"/> instances.</param>
        /// <param name="typeLocator">The type locator use to retrieve all known classes marked with <see cref="EventHandlerAttribute"/>.</param>
        /// <param name="serviceProvider">The service locator used to retrieve singleton event handler dependencies.</param>
        /// <param name="commandPublisher">The command publisher used to publish saga commands.</param>
        private static Dictionary<Type, List<EventHandler>> DiscoverHandleMethods(ILocateTypes typeLocator, IServiceProvider serviceProvider, IStoreSagas sagaStore, Lazy<IPublishCommands> commandPublisher)
        {
            var handlerTypes = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.GetCustomAttribute<EventHandlerAttribute>() != null);
            var knownEventHandlers = new Dictionary<Type, List<EventHandler>>();

            foreach (var handlerType in handlerTypes)
            {
                var handleMethods = GetHandleMethods(handlerType, serviceProvider);
                var sagaMetadata = typeof(Saga).IsAssignableFrom(handlerType) ? GetSagaMetadata(handlerType, handleMethods) : null;

                foreach (var handleMethod in handleMethods)
                {
                    List<EventHandler> eventHandlers;
                    Type eventType = handleMethod.Key;
                    if (!knownEventHandlers.TryGetValue(eventType, out eventHandlers))
                        knownEventHandlers.Add(eventType, eventHandlers = new List<EventHandler>());

                    var eventHandler = new EventHandler(handlerType, eventType, handleMethod.Value, GetHandlerFactory(handlerType, serviceProvider));
                    if (sagaMetadata != null)
                        eventHandler = eventType == typeof(Timeout) ? new SagaTimeoutHandler(eventHandler, sagaMetadata, sagaStore, commandPublisher) : new SagaEventHandler(eventHandler, sagaMetadata, sagaStore, commandPublisher);

                    eventHandlers.Add(eventHandler);
                }
            }

            return knownEventHandlers;
        }
        /// <summary>
        /// Discover all command handlers methods associated with any locatable <see cref="Aggregate"/> type.
        /// </summary>
        /// <param name="typeLocator">The type locator use to retrieve all known <see cref="Aggregate"/> types.</param>
        /// <param name="serviceProvider">The service locator used to retrieve singleton command handler dependencies.</param>
        private static Dictionary<Type, HandleMethodCollection> DiscoverHandleMethods(ILocateTypes typeLocator, IServiceProvider serviceProvider)
        {
            var aggregateTypes = typeLocator.GetTypes(type => !type.IsAbstract && type.IsClass && type.DerivesFrom(typeof(Aggregate)));
            var knownCommandHandlers = new Dictionary<Type, HandleMethodCollection>();

            foreach (var aggregateType in aggregateTypes)
            {
                var handleMethods = GetHandleMethods(aggregateType, serviceProvider);

                knownCommandHandlers.Add(aggregateType, handleMethods);
            }

            return knownCommandHandlers;
        }
        /// <summary>
        /// Generate unique saga type identifiers for all locatable <see cref="Saga"/> types.
        /// </summary>
        /// <param name="typeLocator">The type locator use to retrieve all known <see cref="Saga"/> types.</param>
        private static Dictionary<Type, Guid> GetKnownSagas(ILocateTypes typeLocator)
        {
            var knownSagas = typeLocator.GetTypes(type => type.IsClass && !type.IsAbstract && type.DerivesFrom(typeof(Saga))).ToDictionary(type => type, HashType);
            var logMessage = new StringBuilder();

            logMessage.AppendLine("Discovered sagas:");
            foreach (var saga in knownSagas)
            {
                logMessage.Append("    ");
                logMessage.AppendFormat("{0} - {1}", saga.Key, saga.Value);
                logMessage.AppendLine();
            }

            Log.Debug(logMessage.ToString);

            return knownSagas;
        }