Exemple #1
0
        protected virtual void AddOrUpdateInternal(TEntity entity)
        {
            var key = _keyAccossor.GetKey <TEntity, TKey>(entity);

            if (key == null)
            {
                throw new InvalidOperationException($"object key is missing");
            }

            var item = Collection.Data.FirstOrDefault(FindByKeyPredicate(key));

            if (item != null)
            {
                var index = Collection.Data.IndexOf(item);
                Collection.Data.RemoveAt(index);
                Collection.Data.Insert(index, entity);
            }
            else
            {
                Collection.Data.Add(entity);
            }
        }
        public virtual async Task <TEntity> AddOrUpdate(TEntity entity)
        {
            var id = _keyAccossor.GetKey <TEntity, TKey>(entity);

            if (id == null)
            {
                throw new InvalidOperationException($"object id is missing");
            }
            await Collection.ReplaceOneAsync(GetBsonDocument(id), entity, new UpdateOptions()
            {
                IsUpsert = true
            });

            return(entity);
        }
        public virtual async Task <TEntity> AddOrUpdate(TEntity entity)
        {
            var id = _keyAccossor.GetKey <TEntity, TKey>(entity);

            if (id == null)
            {
                throw new InvalidOperationException($"object key is missing");
            }

            var item = GetById(id);

            if (item != null)
            {
                await item.SyncUpdate(entity);
            }
            else
            {
                await Collection.New(entity).Save();
            }

            return(entity);
        }