public void Add(ReactionInfo reaction) { if (!Contains(reaction)) { List.Add(reaction); } }
/// <summary> /// Loads all the reactions found in the reactions file. /// </summary> /// <param name="includeDisabled"></param> /// <returns>An array of the the reactions found.</returns> public ReactionInfo[] LoadInfoFromFile(bool includeDisabled) { // Logging disabled to boost performance //using (LogGroup logGroup = LogGroup.StartDebug("Loading the reactions from the XML file.")) //{ if (Reactions == null) { List <ReactionInfo> validReactions = new List <ReactionInfo>(); ReactionInfo[] reactions = new ReactionInfo[] {}; using (StreamReader reader = new StreamReader(File.OpenRead(FileNamer.ReactionsInfoFilePath))) { XmlSerializer serializer = new XmlSerializer(typeof(ReactionInfo[])); reactions = (ReactionInfo[])serializer.Deserialize(reader); } foreach (ReactionInfo reaction in reactions) { if (reaction.Enabled || includeDisabled) { validReactions.Add(reaction); } } Reactions = validReactions.ToArray(); } //} return(Reactions); }
/// <summary> /// Initializes the reactions and loads all reactions to state. Note: Skips initialization if already initialized. /// </summary> /// <param name="includeTestReactions"></param> public void Initialize(bool includeTestReactions) { using (LogGroup logGroup = LogGroup.StartDebug("Initializing the business reactions.")) { ReactionInfo[] reactions = new ReactionInfo[] {}; if (!ReactionState.IsInitialized) { if (IsCached) { LogWriter.Debug("Is cached. Loading from XML."); reactions = LoadReactions(); } else { LogWriter.Debug("Is not cached. Scanning from type attributes."); reactions = FindReactions(includeTestReactions); Saver.SaveToFile(reactions); } Initialize(reactions); } else { LogWriter.Debug("Already initialized."); } } }
/// <summary> /// Disposes the reactions found by the scanner. /// </summary> public void Dispose() { using (LogGroup logGroup = LogGroup.Start("Disposing the reactions.", NLog.LogLevel.Debug)) { ReactionInfo[] reactions = new ReactionInfo[] {}; Dispose(ReactionState.Reactions.ToArray()); } }
/// <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 bool Contains(ReactionInfo reaction) { bool found = false; foreach (ReactionInfo r in this) { if (r.ReactionType == reaction.ReactionType) { found = true; } } return(found); }
/// <summary> /// Creates a new instance of the reaction with a Reaction attribute matching the specified type name and action. /// </summary> /// <param name="reactionInfo">The reaction info object that specified the reaction to create.</param> /// <returns>A reaction that is suitable to perform the specified action with the specified type.</returns> public IReaction CreateReaction(ReactionInfo reactionInfo) { IReaction reaction = null; using (LogGroup logGroup = LogGroup.StartDebug("Creating a new reaction based on the provided info.")) { Type reactionType = Type.GetType(reactionInfo.ReactionType); if (reactionType == null) { throw new Exception("Reaction type cannot by instantiated: " + reactionInfo.ReactionType); } Type entityType = null; if (EntityState.IsType(reactionInfo.TypeName)) { entityType = EntityState.GetType(reactionInfo.TypeName); } LogWriter.Debug("Reaction type: " + reactionType.FullName); LogWriter.Debug("Entity type: " + (entityType != null ? entityType.FullName : String.Empty)); LogWriter.Debug("Action: " + reactionInfo.Action); if (entityType != null && reactionType.IsGenericTypeDefinition) { LogWriter.Debug("Is generic type definition."); Type gType = reactionType.MakeGenericType(new Type[] { entityType }); reaction = (IReaction)Activator.CreateInstance(gType); } else { LogWriter.Debug("Is not generic type definition."); reaction = (IReaction)Activator.CreateInstance(reactionType); } if (reaction == null) { throw new ArgumentException("Unable to create instance of reaction: " + entityType.ToString(), "reactionInfo"); } LogWriter.Debug("Reaction created."); } return(reaction); }
/// <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; }
/// <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()); }
/// <summary> /// Retrieves the short type name specified by the Reaction attribute. /// </summary> /// <returns></returns> public virtual string GetTypeName() { ReactionInfo info = new ReactionInfo(this); return(info.TypeName); }
/// <summary> /// Retrieves the action specified by the Reaction attribute. /// </summary> /// <returns></returns> public virtual string GetAction() { ReactionInfo info = new ReactionInfo(this); return(info.Action); }