Esempio n. 1
0
        /// <summary>
        /// Find event handlers from <paramref name="assemblies"/>
        /// </summary>
        /// <param name="assemblies">Assemblies containing handlers</param>
        /// <param name="warmUpHandlerInvokersCache">Warm up invokers cache of found handlers</param>
        /// <returns>The sequence of found handlers descriptors</returns>
        public static IEnumerable <HandlerTypeDescriptor> FindEventHandlersFrom(
            IEnumerable <Assembly> assemblies,
            bool warmUpHandlerInvokersCache = true)
        {
            foreach (var assembly in assemblies)
            {
                var implementations = assembly
                                      .GetTypes()
                                      .Where(type => !type.IsAbstract)
                                      .Select(type => (handlerImplementationType: type,
                                                       handlerTypes: type.GetInterfaces().Where(i =>
                                                                                                i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEventHandler <>))));

                foreach (var(handlerImplementationType, handlerTypes) in implementations)
                {
                    foreach (var handlerType in handlerTypes)
                    {
                        if (warmUpHandlerInvokersCache)
                        {
                            var eventType = ExtractEventType(handlerType);
                            var ctxCtor   =
                                EventDispatcher.EventContextCtorFactory.GetOrCreate(eventType);
                            HandlerInvokerFactory <IEventHandlingContext> .GetOrCreate(ctxCtor.ContextType, handlerType);
                        }

                        yield return(new HandlerTypeDescriptor(handlerType, handlerImplementationType));
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Find command handlers from <paramref name="assemblies"/>
        /// </summary>
        /// <param name="assemblies">Assemblies containing handlers</param>
        /// <param name="warmUpHandlerInvokersCache">Warm up invokers cache of found handlers</param>
        /// <returns>The sequence of found handlers descriptors</returns>
        public static IEnumerable <HandlerTypeDescriptor> FindCommandHandlersFrom(
            IEnumerable <Assembly> assemblies,
            bool warmUpHandlerInvokersCache = true)
        {
            foreach (var assembly in assemblies)
            {
                var implementations = assembly
                                      .GetTypes()
                                      .Where(type => !type.IsAbstract)
                                      .Select(type => (handlerImplementationType: type,
                                                       handlerTypes: type.GetInterfaces().Where(i =>
                                                                                                i.IsGenericType && (i.GetGenericTypeDefinition() == typeof(ICommandHandler <>) ||
                                                                                                                    i.GetGenericTypeDefinition() == typeof(ICommandHandler <,>)))));

                foreach (var(handlerImplementationType, handlerTypes) in implementations)
                {
                    foreach (var handlerType in handlerTypes)
                    {
                        if (warmUpHandlerInvokersCache)
                        {
                            var(commandType, resultType) = ExtractCommandAndResultTypes(handlerType);
                            var ctxCtor =
                                CommandProcessor.CommandContextCtorFactory.GetOrCreate(commandType, resultType);
                            HandlerInvokerFactory <ICommandHandlingContext> .GetOrCreate(ctxCtor.ContextType, handlerType);
                        }

                        yield return(new HandlerTypeDescriptor(handlerType, handlerImplementationType));
                    }
                }
            }
        }