/// <summary> /// The surrogates created as Holders of IStateObjects must be persisted. /// </summary> internal void HandleSurrogateHolders() { //First we need to go through the properties and Adopt any Unattached object. foreach (StateObjectSurrogate obj in _orphanHolders) { var surrogateToAdoptSurrogate = _stateManager.surrogateManager.GetSurrogateFirstReference(obj); var objValue = obj.Value as IStateObject; AdoptionInformation.StaticAdopt(surrogateToAdoptSurrogate, objValue); foreach (var dependant in _stateManager.GetDependentItemsInCache(objValue.UniqueID)) { var pointer = dependant.Value as StateObjectPointer; //If the Target of the opinter is also Unattached. if (pointer != null && !(pointer is UpgradeHelpers.WebMap.Server.StateObjectPointerReferenceSuperValue)) { AdoptionInformation.StaticAdopt(pointer, pointer.Target); } StateManager.Current.StorageManager.SaveObject(dependant.Value); } } //Once all the adoptions accured then we can procceed and store those adopted values. foreach (StateObjectSurrogate obj in _orphanHolders) { var surrogateToAdoptSurrogate = _stateManager.surrogateManager.GetSurrogateFirstReference(obj); var objValue = obj.Value as IStateObject; StateManager.Current.StorageManager.SaveObject(objValue); foreach (var dependant in StateManager.Current.GetDependentItemsInCache(objValue.UniqueID)) { StateManager.Current.StorageManager.SaveObject(dependant.Value); } } _orphanHolders.Clear(); }
private bool TransferValueToReference(string branchRemoved, ReferencesTable referencesTable, IStateObject elementToAdoptObj = null, bool isDispose = false, IFormBaseViewModel formBaseViewModel = null) { if (isDispose && referencesTable.IsReferencedObjectAnIDisposableDependencyControl) { return(false); } //If the elementToAdopt was not sent as a parameter let's calculate it. if (elementToAdoptObj == null) { elementToAdoptObj = referencesTable.ReferencedObject; } string oldParentUniqueID = elementToAdoptObj.UniqueID; //Entire windows won't be preserved. if (elementToAdoptObj is IViewModel) { return(false); } //Let's get the reference candidate for the adoption. var reference = GetReferenceValidForAdoption(referencesTable, false); if (reference == null) { return(false); } var candidate = CalculateCandidateForAdoption(reference); //The cadidates parent was already removed...Ciao if (candidate == null) { return(false); } bool doNotRegisterIDSwitch = true; if (formBaseViewModel == null) { formBaseViewModel = ViewManager.Instance.GetTopLevelObject(elementToAdoptObj) as IFormBaseViewModel; } if (formBaseViewModel != null) { doNotRegisterIDSwitch = formBaseViewModel.IsDisposing; } if (!(elementToAdoptObj is IDisposableDependencyControl) || (!isDispose && !doNotRegisterIDSwitch)) { //The element won't belong to the parent anymore. IStateObject elementsToBeAdopted = _stateManager.DettachObject(elementToAdoptObj, !doNotRegisterIDSwitch); //We need to promote the object and attach it to the first candidate. AdoptionInformation.StaticAdopt(candidate, elementsToBeAdopted); ////Let's update all the references UpdateReferencesTables(oldParentUniqueID, elementToAdoptObj.UniqueID); //Only foreign references allowed referencesTable.RemoveReference(reference); //We move the element to a substitute parent. referencesTable.AttachedToParent = false; //We had a succesfull adoption :) return(true); } return(false); }
private void ProcessIfOrphan() { if (!StateManager.AllBranchesAttached(_current.UniqueID)) { //If we enter here we are Orphans :( //We asumme we got here because we are inside a surrogate and //that means that there is an StateObjectSurrogate associated with the parentObject if (parentCandidate == null) { var typeName = TypeCacheUtils.GetOriginalTypeName(_current.GetType()); throw new NotSupportedException("The wrapped stated of type " + typeName + "object with ID" + _current.UniqueID + " does not have a valid parent reference"); } var parentSurrogate = StateManager.Current.surrogateManager.GetSurrogateFor(parentCandidate, generateIfNotFound: false); if (parentSurrogate == null) { var typeName = TypeCacheUtils.GetOriginalTypeName(_current.GetType()); throw new NotSupportedException("The wrapped stated of type " + typeName + "object with ID" + _current.UniqueID + " does not have a valid parent SURROGATE reference"); } AdoptionInformation.StaticAdopt(parentSurrogate, _current); } }
public static void Serialize(object instance, BinaryWriter binaryWriter, MemoryStream ms, ISurrogateContext context) { var sortedList = instance as System.Collections.SortedList; binaryWriter.Write(sortedList.Count); foreach (System.Collections.DictionaryEntry item in sortedList) { var asStateObject = item.Key as IStateObject; //Flag to determine if value is stateobject binaryWriter.Write(asStateObject != null); //Save Key if (asStateObject != null) { if (!StateManager.AllBranchesAttached(asStateObject.UniqueID)) { var sortedListSurrogate = StateManager.Current.surrogateManager.GetSurrogateFor(sortedList, generateIfNotFound: false); AdoptionInformation.StaticAdopt(sortedListSurrogate, asStateObject); } //TODO this line has to be removed from here when the SaveObject will be generalized StateManager.Current.StorageManager.SaveObject(asStateObject); foreach (var pair in StateManager.Current.GetDependentItemsInCache(asStateObject.UniqueID)) { StateManager.Current.StorageManager.SaveObject(pair.Value); } binaryWriter.Write(asStateObject.UniqueID); } else { binaryWriter.Write(item.Key.ToString()); } //Save Value asStateObject = item.Value as IStateObject; binaryWriter.Write(asStateObject != null); //Save Key if (asStateObject != null) { if (!StateManager.AllBranchesAttached(asStateObject.UniqueID)) { var sortedListSurrogate = StateManager.Current.surrogateManager.GetSurrogateFor(sortedList, generateIfNotFound: false); AdoptionInformation.StaticAdopt(sortedListSurrogate, asStateObject); } //TODO this line has to be removed from here when the SaveObject will be generalized StateManager.Current.StorageManager.SaveObject(asStateObject); foreach (var pair in StateManager.Current.GetDependentItemsInCache(asStateObject.UniqueID)) { StateManager.Current.StorageManager.SaveObject(pair.Value); } binaryWriter.Write(asStateObject.UniqueID); } else { binaryWriter.Write(item.Key.ToString()); } } }