/// <summary> /// Consolidate data from all implementations of <see cref="IGeneratorTargetProvider"/> in loaded /// domain models into a dictionary keyed by generator type. /// </summary> /// <param name="services"></param> /// <returns></returns> public static Dictionary <string, GeneratorTarget[]> ConsolidateGeneratorTargets(IFrameworkServices services) { Dictionary <string, List <GeneratorTarget> > targetsByType = null; foreach (IGeneratorTargetProvider provider in services.GetTypedDomainModelProviders <IGeneratorTargetProvider>()) { foreach (IGeneratorTargetList targetList in provider.GetGeneratorTargets()) { IList <GeneratorTarget> localTargets; int targetCount; if (null != (localTargets = targetList.GeneratorTargets) && 0 != (targetCount = localTargets.Count)) { for (int i = 0; i < targetCount; ++i) { GeneratorTarget target = localTargets[i]; string targetType = target.TargetType; List <GeneratorTarget> combinedTargets; if (null == targetsByType) { targetsByType = new Dictionary <string, List <GeneratorTarget> >(); targetsByType[targetType] = combinedTargets = new List <GeneratorTarget>(); } else if (!targetsByType.TryGetValue(targetType, out combinedTargets)) { combinedTargets = new List <GeneratorTarget>(); targetsByType[targetType] = combinedTargets; } combinedTargets.Add(target); } } } } // This returns a dictionary keyed by a generator type // with array values corresponding to unique GeneratorType // instances for that target. Dictionary <string, GeneratorTarget[]> serializeableResult = null; if (targetsByType != null) { Dictionary <GeneratorTarget, object> keyedByTarget = new Dictionary <GeneratorTarget, object>(); serializeableResult = new Dictionary <string, GeneratorTarget[]>(); foreach (KeyValuePair <string, List <GeneratorTarget> > kvp in targetsByType) { List <GeneratorTarget> targetList = kvp.Value; keyedByTarget.Clear(); int i; int count; for (i = 0, count = targetList.Count; i < count; ++i) { // This limits any duplicate key to a single entry keyedByTarget[targetList[i]] = null; } count = keyedByTarget.Count; GeneratorTarget[] targets = new GeneratorTarget[count]; serializeableResult[kvp.Key] = targets; keyedByTarget.Keys.CopyTo(targets, 0); } } return(serializeableResult); }
/// <summary> /// Typed Equals method /// </summary> public bool Equals(GeneratorTarget obj) { // Note that the id is intentionally ignored for equality to allow an // instance to be used as a type/name key. return(TargetType == obj.TargetType && StringComparer.OrdinalIgnoreCase.Equals(TargetName, obj.TargetName)); }