/// <summary> /// Links the <c>item</c> to the <see cref="instanceRegistry"/> (overwrite any existing) /// </summary> /// <param name="item">The item.</param> /// <param name="key">The key.</param> /// <exception cref="ArgumentOutOfRangeException">item - The ObjectTable is not in linkable mode - IsInstanceLinkActive = false</exception> protected virtual void LinkBase(object item, string key = null) { if (!IsInstanceLinkActive) { throw new ArgumentOutOfRangeException("item", item, "The ObjectTable is not in linkable mode - IsInstanceLinkActive = false"); } if (item == null) { return; } if (item is IObjectTableEntry) { if (instanceRegistry.ContainsKey(key)) { instanceRegistry.TryAdd(key, item as IObjectTableEntry); } else { if (key.isNullOrEmpty()) { key = GetKeyValue(item); } IObjectTableEntry removedItem = null; instanceRegistry.TryRemove(key, out removedItem); instanceRegistry.TryAdd(key, item as IObjectTableEntry); } } }
/// <summary> /// Removes the specified item. /// </summary> /// <param name="item">The item.</param> /// <returns></returns> protected bool Remove(object item) { if (ReadOnlyEnforce()) { return(false); } if (ContainsObject(item)) { string keyValue = GetKeyValue(item); if (keyCache.Contains(keyValue)) { keyCache.TryTake(out keyValue); } string select_exp = GetPrimaryKeySelect(keyValue); DataRow[] rows = tableSelect(select_exp); if (rows.Any()) { lock (ContainsKeyLock) { foreach (DataRow dr in rows) { table.Rows.Remove(dr); } } if (IsInstanceLinkActive) { IObjectTableEntry removedItem = null; instanceRegistry.TryRemove(keyValue, out removedItem); return(removedItem != null); } return(true); } else { return(false); } } else { return(false); } }
/// <summary> /// Adds the or updates. Returns <c>true</c> if new row was added /// </summary> /// <param name="input">The input.</param> /// <param name="policy">The policy.</param> /// <returns></returns> protected bool AddOrUpdate(object input, objectTableUpdatePolicy policy = objectTableUpdatePolicy.overwrite) { if (ReadOnlyEnforce()) { return(false); } if (input == null) { return(false); } if (!input.GetType().isCompatibileWith(type)) { return(false); } bool output = false; string keyValue = GetKeyValue(input); //if (keyValue.isNullOrEmpty()) //{ // aceLog.log("keyValue is null => " + name + " : " + GetType().Name); //} if (IsInstanceLinkActive) { if (instanceRegistry.ContainsKey(keyValue)) { IObjectTableEntry item = input as IObjectTableEntry; if (instanceRegistry.TryUpdate(keyValue, item, item)) { output = false; } } } Boolean _exists = false; DataRow[] rows = null; if (!WriteOnlyEnforce()) { string select_exp = GetPrimaryKeySelect(keyValue); rows = tableSelect(select_exp); _exists = rows.Any(); } if (_exists) { SetRowWithObject(rows.First(), input, policy); output = false; } else { SetRowWithObject(null, input, objectTableUpdatePolicy.overwrite); output = true; } if (IsInstanceLinkActive) { if (!instanceRegistry.ContainsKey(keyValue)) { IObjectTableEntry item = input as IObjectTableEntry; if (instanceRegistry.TryAdd(keyValue, item)) { output = true; } } } if (output) { lastEntry = input; } return(output); }