/// <summary> /// Locates the strategy info 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 strategy.</param> /// <param name="type">The type that is involved in the action.</param> /// <returns>The strategy info for the specified scenario.</returns> public StrategyInfo LocateFromInterfaces(string action, Type type) { StrategyInfo strategyInfo = null; //using (LogGroup logGroup = LogGroup.Start("Locating a strategy by checking the interfaces of the provided type.", NLog.LogLevel.Debug)) //{ string key = StrategyInfo.GetStrategyKey(action, type.Name); if (type.IsInterface && Strategies.ContainsKey(key)) { strategyInfo = Strategies[key]; } else { Type[] interfaceTypes = type.GetInterfaces(); // Loop backwards through the interface types for (int i = interfaceTypes.Length - 1; i >= 0; i--) { // If a strategy is already found then skip the rest if (strategyInfo == null) { Type interfaceType = interfaceTypes[i]; // using (LogGroup logGroup2 = LogGroup.Start("Checking interface: " + interfaceType.FullName, NLog.LogLevel.Debug)) // { string key2 = StrategyInfo.GetStrategyKey(action, interfaceType.Name); // LogWriter.Debug("Key: " + key); if (Strategies.ContainsKey(key2)) { strategyInfo = Strategies[key2]; // LogWriter.Debug("Strategy found: " + strategyInfo.StrategyType); } // else // LogWriter.Debug("No strategy found for that key."); // } } } } //} return(strategyInfo); }
/// <summary> /// Locates the strategy info 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 strategy.</param> /// <param name="type">The type that is involved in the action.</param> /// <returns>The strategy info for the specified scenario.</returns> public StrategyInfo LocateFromHeirarchy(string action, Type type) { StrategyInfo strategyInfo = null; //using (LogGroup logGroup = LogGroup.Start("Locating a strategy by navigating the hierarchy of the provided type.", NLog.LogLevel.Debug)) //{ strategyInfo = LocateFromInterfaces(action, type); if (strategyInfo == null) { strategyInfo = LocateFromBaseTypes(action, type); } // LogWriter.Debug("Strategy found: " + (strategyInfo != null ? strategyInfo.StrategyType : "[null]")); // LogWriter.Debug("Strategy key: " + (strategyInfo != null ? strategyInfo.Key : "[null]")); //} return(strategyInfo); }
/// <summary> /// Locates the strategy info for performing the specified action with the specified type. /// </summary> /// <param name="action">The action that is to be performed by the strategy.</param> /// <param name="typeName">The short type name of the entity that is involved in the action.</param> /// <returns>The strategy info for the specified scenario.</returns> public StrategyInfo Locate(string action, string typeName) { // Create the strategy info variable to hold the return value StrategyInfo strategyInfo = null; //using (LogGroup logGroup = LogGroup.Start("Locating the strategy that is appropriate for carrying out the action '" + action + "' involving the type '" + typeName + "'.", NLog.LogLevel.Debug)) //{ // Get the specified type Type type = null; if (EntityState.IsType(typeName)) { type = EntityState.GetType(typeName); } // Create a direct strategy key for the specified type string key = StrategyInfo.GetStrategyKey(action, typeName); // LogWriter.Debug("Direct key: " + key); // LogWriter.Debug("Type name: " + typeName); // Check the direct key to see if a strategy exists if (Strategies.ContainsKey(key)) { // LogWriter.Debug("Found strategy with key: " + key); strategyInfo = Strategies[key]; } // If not then navigate up the heirarchy looking for a matching strategy else 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."); strategyInfo = LocateFromHeirarchy(action, type); } // LogWriter.Debug("Strategy found: " + (strategyInfo != null ? strategyInfo.StrategyType : "[null]")); // LogWriter.Debug("Strategy key: " + (strategyInfo != null ? strategyInfo.Key : "[null]")); //} return(strategyInfo); }
/// <summary> /// Locates the strategy 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 strategy.</param> /// <param name="type">The type that is involved in the action.</param> /// <returns>The strategy info for the specified scenario.</returns> public StrategyInfo LocateFromBaseTypes(string action, Type type) { StrategyInfo strategyInfo = null; //using (LogGroup logGroup = LogGroup.Start("Locating strategy via the base types of the provided type.", NLog.LogLevel.Debug)) //{ TypeNavigator navigator = new TypeNavigator(type); while (navigator.HasNext && strategyInfo == null) { Type nextType = navigator.Next(); if (strategyInfo == null) { // using (LogGroup logGroup2 = LogGroup.Start("Checking base type: " + nextType.FullName, NLog.LogLevel.Debug)) // { string key = StrategyInfo.GetStrategyKey(action, nextType.Name); // LogWriter.Debug("Key: " + key); // If a strategy exists for the base type then use it if (Strategies.StrategyExists(key)) { strategyInfo = Strategies[key]; // LogWriter.Debug("Strategy found: " + strategyInfo.StrategyType); } // TODO: Check if needed. It shouldn't be. The other call to LocateFromInterfaces in LocateFromHeirarchy should be sufficient // Otherwise check the interfaces of that base type //else //{ // strategyInfo = LocateFromInterfaces(action, nextType); //} // } } } //} return(strategyInfo); }
/// <summary> /// Creates a new authorise reference strategy. /// </summary> /// <param name="typeName"></param> /// <returns></returns> public IAuthoriseReferenceStrategy NewReferenceAuthoriser(string typeName1, string propertyName1, string typeName2, string propertyName2) { IAuthoriseReferenceStrategy strategy = null; using (LogGroup logGroup = LogGroup.StartDebug("Creating a new authorise reference strategy.")) { LogWriter.Debug("Type name 1: " + typeName1); LogWriter.Debug("Property name 1: " + propertyName1); LogWriter.Debug("Type name 2: " + typeName2); LogWriter.Debug("Property name 2: " + propertyName2); AuthoriseReferenceStrategyLocator locator = new AuthoriseReferenceStrategyLocator(StrategyState.Strategies); StrategyInfo strategyInfo = locator.Locate(typeName1, propertyName1, typeName2, propertyName2); if (strategyInfo != null) { strategy = strategyInfo.New <IAuthoriseReferenceStrategy>(); } } return(strategy); }
/// <summary> /// Loads the strategy from the specified path. /// </summary> /// <param name="strategyPath">The full path to the strategy to load.</param> /// <returns>The strategy deserialized from the specified file path.</returns> public StrategyInfo LoadFromFile(string strategyPath) { StrategyInfo info = null; // Logging disabled to boost performance //using (LogGroup logGroup = LogGroup.StartDebug("Loading the strategy from the specified path.")) //{ if (!File.Exists(strategyPath)) { throw new ArgumentException("The specified file does not exist."); } // LogWriter.Debug("Path: " + strategyPath); Type type = typeof(StrategyInfo); string fileName = Path.GetFileName(strategyPath); // If the info file name contains the '.ar' sub extension then it's an authorise reference strategy if (fileName.IndexOf(".ar.strategy") > -1) { type = typeof(AuthoriseReferenceStrategyInfo); } using (StreamReader reader = new StreamReader(File.OpenRead(strategyPath))) { XmlSerializer serializer = new XmlSerializer(type); info = (StrategyInfo)serializer.Deserialize(reader); reader.Close(); } //} return(info); }