/// <summary> /// Find the id that is present in the given <paramref name="baseIds"/> collection but absent from this instance. /// </summary> /// <param name="baseIds">The collection of id that contains one id missing in this collection.</param> /// <returns>The id present in <paramref name="baseIds"/> that is absent from this instance.</returns> /// <exception cref="InvalidOperationException">Multiple ids are missing in this instance.</exception> public IEnumerable <ItemId> FindMissingIds(CollectionItemIdentifiers baseIds) { // Create an hashset with all ids, deleted or active, from this instance. var hashSet = new HashSet <ItemId>(deletedItems); foreach (var item in keyToIdMap) { hashSet.Add(item.Value); } foreach (var item in baseIds.keyToIdMap) { // Find an active id present in the baseIds that is not part of the hashset for this. if (!hashSet.Contains(item.Value)) { yield return(item.Value); } } foreach (var item in baseIds.deletedItems) { // Find a deleted instance id in the baseIds that is not part of the hashset for this. if (!hashSet.Contains(item)) { yield return(item); } } }
public void CloneInto(CollectionItemIdentifiers target, IReadOnlyDictionary <object, object> referenceTypeClonedKeys) { target.keyToIdMap.Clear(); target.deletedItems.Clear(); foreach (var key in keyToIdMap) { object clonedKey; if (key.Key.GetType().IsValueType || referenceTypeClonedKeys == null) { target.Add(key.Key, key.Value); } else if (referenceTypeClonedKeys.TryGetValue(key.Key, out clonedKey)) { target.Add(clonedKey, key.Value); } else { throw new KeyNotFoundException("Unable to find the non-value type key in the dictionary of cloned keys."); } } foreach (var deletedItem in DeletedItems) { target.MarkAsDeleted(deletedItem); } }
public static bool TryGetCollectionItemIds(object instance, out CollectionItemIdentifiers itemIds) { var shadow = ShadowObject.Get(instance); if (shadow == null) { itemIds = null; return(false); } object result; itemIds = shadow.TryGetValue(CollectionItemIdKey, out result) ? (CollectionItemIdentifiers)result : null; return(result != null); }
/// <summary> /// Find the id that is present in the given <paramref name="baseIds"/> collection but absent from this instance. /// </summary> /// <param name="baseIds">The collection of id that contains one id missing in this collection.</param> /// <returns>The id present in <paramref name="baseIds"/> that is absent from this instance.</returns> /// <exception cref="InvalidOperationException">Multiple ids are missing in this instance.</exception> public ItemId FindMissingId(CollectionItemIdentifiers baseIds) { // Create an hashset with all ids, deleted or active, from this instance. var hashSet = new HashSet <ItemId>(deletedItems); foreach (var item in keyToIdMap) { hashSet.Add(item.Value); } var missingId = ItemId.Empty; foreach (var item in baseIds.keyToIdMap) { // Find an active id present in the baseIds that is not part of the hashset for this. if (!hashSet.Contains(item.Value)) { // TODO: if we have scenario where this is ok, I guess we can just return the first one. if (missingId != ItemId.Empty) { throw new InvalidOperationException("Multiple ids are missing."); } missingId = item.Value; } } // Find an deleted instance id in the baseIds that is not part of the hashset for this. foreach (var item in baseIds.deletedItems) { if (!hashSet.Contains(item)) { // TODO: if we have scenario where this is ok, I guess we can just return the first one. if (missingId != ItemId.Empty) { throw new InvalidOperationException("Multiple ids are missing."); } missingId = item; } } return(missingId); }
public static CollectionItemIdentifiers GetCollectionItemIds(object instance) { if (instance.GetType().IsValueType) { throw new ArgumentException(@"The given instance is a value type and cannot have a item ids attached to it.", nameof(instance)); } var shadow = ShadowObject.GetOrCreate(instance); object result; if (shadow.TryGetValue(CollectionItemIdKey, out result)) { return((CollectionItemIdentifiers)result); } var itemIds = new CollectionItemIdentifiers(); shadow.Add(CollectionItemIdKey, itemIds); return(itemIds); }