/// <summary> /// Updates an entity in the collection, copies properties from staged item /// </summary> /// <param name="stagedItem"></param> /// <returns>if success return 1, else 0</returns> private int UpdateCollection(StagedItem <TEntity> stagedItem) { var updatedEntity = stagedItem.Entity; var updatedEntityKey = (int)updatedEntity.GetType().GetProperty(stagedItem.PrimaryKeyName).GetValue(updatedEntity); if (!Exist(updatedEntityKey)) { return(0); } var collection = GetCollection(updatedEntity.GetType()); if (collection == null) { return(0); } var index = 0; //find the index of entity with the same key foreach (var entity in collection) { var currentEntitykey = (int)entity.GetType().GetProperty(stagedItem.PrimaryKeyName).GetValue(entity); if (currentEntitykey == updatedEntityKey) { break; } index++; } collection[index] = stagedItem.Entity; return(1); }
/// <summary> /// Adds an entity to collection, copies entities from staged item /// </summary> /// <param name="stagedItem"></param> /// <returns> if success return 1, else 0</returns> private int AddToCollection(StagedItem <TEntity> stagedItem) { var entity = stagedItem.Entity; var key = (int)entity.GetType().GetProperty(stagedItem.PrimaryKeyName).GetValue(entity); if (Exist(key)) { return(0); } var collection = GetCollection(entity.GetType()); if (collection == null) { return(0); } MethodInfo addMethod = collection.GetType().GetMethod("Add"); addMethod.Invoke(collection, new object[] { entity }); return(1); }