/// <summary> /// Dehydrates a runtime object model <see cref="FieldAssociation"/> to a persistence object. /// </summary> /// <param name="association"> /// The association. /// </param> /// <returns> /// The <see cref="FieldAssociationData"/>. /// </returns> private static FieldAssociationData ConvertAndCache(FieldAssociation association) { if (association == null) { throw new ArgumentNullResourceException("association", Resources.General_Given_Parameter_Cannot_Be_Null); } FieldAssociationData converted; if (association is StaticAssociation) { converted = new StaticAssociationData(); } else if (association is ConsumeAssociation) { converted = new ConsumeAssociationData(); } else { converted = new FieldAssociationData(); } converted.Name = association.Name; VisualisableTypeDataContainer typeData; if (!FieldDataCache.TryGetValue(association.AssociatedTo.Id, out typeData)) { typeData = new VisualisableTypeDataContainer(); FieldDataCache.Add(association.AssociatedTo.Id, typeData); // Stack Overflow potential, be sure to cache converted types to ensure no circular references. typeData.Data = association.AssociatedTo.ExtractPersistentData(); } if (typeData.Data == null) { // Should be free from memory leaks being a private class typeData.DataReady += (s, e) => converted.AssociatedTo = typeData.Data; } converted.AssociatedTo = typeData.Data; converted.UsageCount = association.UsageCount; return converted; }
private static ParentAssociationData ConvertAndCache(ParentAssociation parentAssociation) { if (parentAssociation == null) { throw new ArgumentNullResourceException("parentAssociation", Resources.General_Given_Parameter_Cannot_Be_Null); } VisualisableTypeDataContainer typeData; if (!FieldDataCache.TryGetValue(parentAssociation.AssociatedTo.Id, out typeData)) { typeData = new VisualisableTypeDataContainer(); FieldDataCache.Add(parentAssociation.AssociatedTo.Id, typeData); typeData.Data = parentAssociation.AssociatedTo.ExtractPersistentData(); // Stack Overflow potential, be sure to cache converted types to ensure no circular references. } var parentData = new ParentAssociationData { Name = parentAssociation.Name, AssociatedTo = typeData.Data }; if (typeData.Data == null) { // Should be free from memory leaks being a private class typeData.DataReady += (s, e) => parentData.AssociatedTo = typeData.Data; } // This must be stored in the cache before ExtractPersistentData is called, which is a recursive call and could cause infinite recursion. return parentData; }