Example #1
0
        private static void MapMessageTypeToSagaType(Type messageType, Type sagaType)
        {
            List <Type> sagas;

            MessageTypeToSagaTypesLookup.TryGetValue(messageType, out sagas);

            if (sagas == null)
            {
                sagas = new List <Type>(1);
                MessageTypeToSagaTypesLookup[messageType] = sagas;
            }

            if (!sagas.Contains(sagaType))
            {
                sagas.Add(sagaType);
            }

            IDictionary <Type, MethodInfo> methods;

            SagaTypeToHandleMethodLookup.TryGetValue(sagaType, out methods);

            if (methods == null)
            {
                methods = new Dictionary <Type, MethodInfo>();
                SagaTypeToHandleMethodLookup[sagaType] = methods;
            }

            Type directType = typeof(IMessageHandler <>).MakeGenericType(messageType);

            if (directType.IsAssignableFrom(sagaType))
            {
                methods[messageType] = directType.GetMethod("Handle", new[] { messageType });
            }
        }
Example #2
0
        /// <summary>
        /// Gets a reference to the generic "Handle" method on the given saga
        /// for the given message type using a hashtable lookup rather than reflection.
        /// </summary>
        /// <param name="saga"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static MethodInfo GetHandleMethodForSagaAndMessage(object saga, IMessage message)
        {
            IDictionary <Type, MethodInfo> lookup;

            SagaTypeToHandleMethodLookup.TryGetValue(saga.GetType(), out lookup);

            if (lookup == null)
            {
                return(null);
            }

            foreach (Type messageType in lookup.Keys)
            {
                if (messageType.IsAssignableFrom(message.GetType()))
                {
                    return(lookup[messageType]);
                }
            }

            return(null);
        }