Пример #1
0
        public async Task <T> InsertOrUpdateAsync(T entity, Guid modifiedBy)
        {
            var existedEntity = await FindOneAsync(t => t.Id == entity.Id);

            if (existedEntity == null)
            {
                entity.Created   = DateTime.UtcNow;
                entity.CreatedBy = modifiedBy;
                entity.Version   = 1;
                await Context.Set <T>().AddAsync(entity);
            }
            else
            {
                if (existedEntity.Version != entity.Version)
                {
                    throw existedEntity.Id.VersionInNotCorrectException();
                }

                entity.Modified   = DateTimeProvider.GetCurrentDateTime();
                entity.ModifiedBy = modifiedBy;
                entity.CreatedBy  = existedEntity.CreatedBy;
                entity.Created    = existedEntity.Created;
                entity.IsDeleted  = false;
                entity.Version   += 1;
                Context.Entry(existedEntity).State = EntityState.Detached;
                Context.Entry(entity).State        = EntityState.Modified;
                Context.Set <T>().Update(entity);
            }
            await Context.SaveChangesAsync();

            return(entity);
        }
Пример #2
0
        public async Task DeleteAsync(Guid id, Guid modifiedBy, bool hardDelete = false)
        {
            var entity = await Context.Set <T>().FindAsync(id);

            if (entity == null || entity.IsDeleted)
            {
                throw new DalException(ErrorCodes.EntityNotFound, id);
            }

            if (hardDelete)
            {
                Context.Set <T>().Remove(entity);
                await Context.SaveChangesAsync();
            }
            else
            {
                entity.IsDeleted  = true;
                entity.Modified   = DateTimeProvider.GetCurrentDateTime();
                entity.ModifiedBy = modifiedBy;
                Context.Set <T>().Update(entity);
                await Context.SaveChangesAsync();
            }
        }