예제 #1
0
 public void Remove(ReactionInfoCollection reactions)
 {
     if (reactions != null)
     {
         Remove(reactions.ToArray());
     }
 }
예제 #2
0
        /// <summary>
        /// Locates the reaction infos for performing the specified action with the specified type by looking at the interfaces of the provided type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the reaction.</param>
        /// <param name="type">The type that is involved in the action.</param>
        /// <returns>The reaction info for the specified scenario.</returns>
        public ReactionInfo[] LocateFromInterfaces(string action, Type type)
        {
            ReactionInfoCollection reactionInfos = new ReactionInfoCollection();

            // Logging commented out to boost performance
            //using (LogGroup logGroup = LogGroup.StartDebug("Locating a reaction by checking the interfaces of the provided type."))
            //{
            Type[] interfaceTypes = type.GetInterfaces();

            // Loop backwards through the interface types
            for (int i = interfaceTypes.Length - 1; i >= 0; i--)
            {
                Type interfaceType = interfaceTypes[i];

                //using (LogGroup logGroup2 = LogGroup.StartDebug("Checking interface: " + interfaceType.FullName))
                //{
                string key = Reactions.GetReactionsKey(action, interfaceType.Name);

                //LogWriter.Debug("Key: " + key);

                if (Reactions.ReactionExists(key))
                {
                    reactionInfos.AddRange(Reactions[key]);

                    //LogWriter.Debug("Reactions found: " + reactionInfos.Count.ToString());
                }
                //else
                //LogWriter.Debug("No reaction found for that key.");
                //}
            }
            //}
            return(reactionInfos.ToArray());
        }
예제 #3
0
        /// <summary>
        /// Sets the reactions with the provided action and type.
        /// </summary>
        /// <param name="action">The action that the reaction performs.</param>
        /// <param name="type">The type of entity involved in the reaction</param>
        /// <param name="reactions">The reactions that correspond with the specified action and type.</param>
        public void SetReactions(string action, string type, ReactionInfoCollection reactions)
        {
            if (reactions == null)
            {
                reactions = new ReactionInfoCollection();
            }

            SetReactions(action, type, reactions.ToArray());
        }
예제 #4
0
        /// <summary>
        /// Sets the reaction with the provided action and type.
        /// </summary>
        /// <param name="action">The action that the reaction performs.</param>
        /// <param name="type">The type of entity involved in the reaction</param>
        /// <param name="reaction">The reaction that corresponds with the specified action and type.</param>
        public void SetReaction(string action, string type, ReactionInfo reaction)
        {
            ReactionInfoCollection reactions = new ReactionInfoCollection();

            reactions.AddRange(this[GetReactionsKey(action, type)]);

            reactions.Add(reaction);

            this[GetReactionsKey(action, type)] = reactions;
        }
 public void AddRange(ReactionInfoCollection reactions)
 {
     if (reactions != null)
     {
         foreach (ReactionInfo reaction in reactions.ToArray())
         {
             Add(reaction);
         }
     }
 }
예제 #6
0
        /// <summary>
        /// Creates a new instance of the reaction with a Reaction attribute matching the specified type name and action.
        /// </summary>
        /// <param name="action">The action that the new reaction will be performing.</param>
        /// <param name="typeName">The name of the type involved in the action.</param>
        /// <returns>A reaction that is suitable to perform the specified action with the specified type.</returns>
        public IReaction[] NewReactions(string action, string typeName)
        {
            IReaction[] reactions = new IReaction[] {};

            ReactionInfoCollection infos = ReactionState.Reactions[action, typeName];

            reactions = infos.New(action, typeName);

            return(reactions);
        }
예제 #7
0
        /// <summary>
        /// Locates the reaction info for performing the specified action with the specified type by looking at the base types of the provided type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the reaction.</param>
        /// <param name="type">The type that is involved in the action.</param>
        /// <returns>The reaction info for the specified scenario.</returns>
        public ReactionInfo[] LocateFromBaseTypes(string action, Type type)
        {
            ReactionInfoCollection reactionInfos = new ReactionInfoCollection();

            // Logging commented out to boost performance
            //using (LogGroup logGroup = LogGroup.StartDebug("Locating reaction via the base types of the '" + (type != null ? type.FullName : "[null]") + "' type."))
            //{
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (action == String.Empty)
            {
                throw new ArgumentException("An action must be specified.");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            TypeNavigator navigator = new TypeNavigator(type);

            while (navigator.HasNext)
            {
                Type nextType = navigator.Next();

                if (nextType != null)
                {
                    //using (LogGroup logGroup2 = LogGroup.StartDebug("Checking base type: " + nextType.FullName))
                    //{
                    string key = Reactions.GetReactionsKey(action, nextType.Name);

                    //LogWriter.Debug("Key: " + key);

                    // If a reaction exists for the base type then use it
                    if (Reactions.ReactionExists(key))
                    {
                        if (Reactions.ContainsKey(key))
                        {
                            reactionInfos.AddRange(Reactions[key]);
                        }

                        //LogWriter.Debug("Reactions found: " + reactionInfos.Count.ToString());
                    }
                    //}
                }
            }

            //}
            return(reactionInfos.ToArray());
        }
예제 #8
0
        /// <summary>
        /// Locates the reaction infos for performing the specified action with the specified type by looking at the base types and interfaces of the provided type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the reaction.</param>
        /// <param name="type">The type that is involved in the action.</param>
        /// <returns>The reaction info for the specified scenario.</returns>
        public ReactionInfo[] LocateFromHeirarchy(string action, Type type)
        {
            ReactionInfoCollection reactionInfos = new ReactionInfoCollection();

            // Logging commented out to boost performance
            //using (LogGroup logGroup = LogGroup.StartDebug("Locating a reaction by navigating the hierarchy of the provided type."))
            //{
            reactionInfos.AddRange(LocateFromInterfaces(action, type));

            reactionInfos.AddRange(LocateFromBaseTypes(action, type));
            //}
            return(reactionInfos.ToArray());
        }
예제 #9
0
        /// <summary>
        /// Adds the provided reaction info to the collection.
        /// </summary>
        /// <param name="reaction">The reaction info to add to the collection.</param>
        public void Add(ReactionInfo reaction)
        {
            if (reaction == null)
            {
                throw new ArgumentNullException("reaction");
            }

            string key = GetReactionsKey(reaction.Action, reaction.TypeName);

            ReactionInfoCollection list = new ReactionInfoCollection();

            if (ContainsKey(key))
            {
                list.AddRange(this[key]);
            }

            list.Add(reaction);

            this[key] = list;
        }
예제 #10
0
        /// <summary>
        /// Retrieves the reactions with the provided action and type.
        /// </summary>
        /// <param name="action">The action that the reaction performs.</param>
        /// <param name="typeName">The type of entity involved in the reaction</param>
        /// <returns>The reaction matching the provided action and type.</returns>
        public ReactionInfoCollection GetReactions(string action, string typeName)
        {
            ReactionInfoCollection foundReactions = new ReactionInfoCollection();

            using (LogGroup logGroup = LogGroup.StartDebug("Retrieving the reactions to the action '" + action + "' with the type '" + typeName + "'."))
            {
                if (action == null)
                {
                    throw new ArgumentNullException("action");
                }

                if (typeName == null)
                {
                    throw new ArgumentNullException("typeName");
                }


                if (action == String.Empty)
                {
                    throw new ArgumentException("An action must be provided other than String.Empty.", "action");
                }

                if (typeName == String.Empty)
                {
                    throw new ArgumentException("A type name must be provided other than String.Empty.", "typeName");
                }

                ReactionLocator locator = new ReactionLocator(this);

                foundReactions = locator.Locate(action, typeName);

                if (foundReactions == null)
                {
                    throw new ReactionNotFoundException(action, typeName);
                }
            }

            return(foundReactions);
        }
예제 #11
0
        /// <summary>
        /// Finds all the strategies in the available assemblies.
        /// </summary>
        /// <param name="includeTestReactions"></param>
        /// <returns>An array of info about the strategies found.</returns>
        public ReactionInfo[] FindReactions(bool includeTestReactions)
        {
            ReactionInfoCollection strategies = new ReactionInfoCollection();

            //using (LogGroup logGroup = LogGroup.Start("Finding strategies by scanning the attributes of the available type.", NLog.LogLevel.Debug))
            //{
            foreach (string assemblyPath in AssemblyPaths)
            {
                Assembly assembly = Assembly.LoadFrom(assemblyPath);

                if (ContainsReactions(assembly, includeTestReactions))
                {
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (IsReaction(type))
                        {
                            //LogWriter.Debug("Found reaction type: " + type.ToString());

                            ReactionInfo reactionInfo = new ReactionInfo(type);

                            if (reactionInfo.TypeName != null && reactionInfo.TypeName != String.Empty &&
                                reactionInfo.Action != null && reactionInfo.Action != String.Empty)
                            {
                                //LogWriter.Debug("Found match.");

                                //LogWriter.Debug("Type name: " + reactionInfo.TypeName);
                                //LogWriter.Debug("Action: " + reactionInfo.Action);

                                strategies.Add(reactionInfo);
                            }
                        }
                    }
                }
            }
            //}

            return(strategies.ToArray());
        }
예제 #12
0
 /// <summary>
 /// Sets the reactions with the provided action and type.
 /// </summary>
 /// <param name="action">The action that the reaction performs.</param>
 /// <param name="type">The type of entity involved in the reaction</param>
 /// <param name="reactions">The reactions that correspond with the specified action and type.</param>
 public void SetReactions(string action, string type, ReactionInfo[] reactions)
 {
     this[GetReactionsKey(action, type)] = new ReactionInfoCollection(reactions);
 }
예제 #13
0
        /// <summary>
        /// Locates the reaction infos for performing the specified action with the specified type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the reaction.</param>
        /// <param name="typeName">The short type name of the entity that is involved in the action.</param>
        /// <returns>The reaction info for the specified scenario.</returns>
        public ReactionInfoCollection Locate(string action, string typeName)
        {
            // Create the reaction info variable to hold the return value
            ReactionInfoCollection reactionInfos = new ReactionInfoCollection();

            // Logging commented out to boost performance
            //using (LogGroup logGroup = LogGroup.StartDebug("Locating the reaction that is appropriate for carrying out the action '" + action + "' involving the type '" + typeName + "'."))
            //{
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }


            if (action == String.Empty)
            {
                throw new ArgumentException("An action must be provided other than String.Empty.", "action");
            }

            if (typeName == String.Empty)
            {
                throw new ArgumentException("A type name must be provided other than String.Empty.", "typeName");
            }

            // Get the specified type
            Type type = null;

            if (EntityState.IsType(typeName))
            {
                type = EntityState.GetType(typeName);
            }

            // Create a direct reaction key for the specified type
            string key = Reactions.GetReactionsKey(action, typeName);

            //	LogWriter.Debug("Direct key: " + key);
            //	LogWriter.Debug("Type name: " + typeName);

            // Check the direct key to see if a reaction exists
            if (Reactions.ReactionExists(key))
            {
                LogWriter.Debug("Found reaction with key: " + key);

                reactionInfos.AddRange(Reactions[key]);
            }

            // Navigate up the heirarchy looking for a matching reaction
            // This is done even if reactions have already been found
            if (type != null)                     // If no type was found then skip the hierarchy check as it's just a name without a corresponding type
            {
                //		LogWriter.Debug("Not found with direct key. Looking through the hierarchy.");

                reactionInfos.AddRange(LocateFromHeirarchy(action, type));
            }

            //}

            return(reactionInfos);
        }