Esempio n. 1
0
        public virtual async Task <List <TEntity> > Get(Expression <Func <TEntity, bool> > predicate = null,
                                                        bool disableTracking = true,
                                                        Func <IQueryable <TEntity>, IIncludableQueryable <TEntity, object> > include = null)
        {
            var entityQuery = DB.Set <TEntity>()
                              .AsQueryable()
            ;

            if (predicate != null)
            {
                entityQuery = entityQuery.Where(predicate);
            }

            if (disableTracking)
            {
                entityQuery = entityQuery.AsNoTracking();
            }

            // We assign all passed includes (if there are any)
            entityQuery = entityQuery.AssignIncludes(include);
            var entitiesList = await entityQuery.ToListAsync();

            ProjectExceptions.ThrowNotFoundIfNull(entitiesList);

            return(entitiesList);
        }
Esempio n. 2
0
        public virtual async Task <TEntity> Save(TEntityDTO data, bool autoSave = true, Func <IQueryable <TEntity>, IIncludableQueryable <TEntity, object> > include = null)
        {
            var isNewEntity = data.Id == null || data.Id.Equals(Guid.Empty);

            var entityToSave = isNewEntity
                ? new TEntity()
                : await GetById(data.Id ?? Guid.Empty, false, include)
            ;

            ProjectExceptions.ThrowNotFoundIfNull(entityToSave);

            Mapper.Map(data, entityToSave);


            if (isNewEntity)
            {
                await IsNewEntity(data, entityToSave);

                DB.Set <TEntity>().Add(entityToSave);
            }
            else
            {
                await IsExistingEntity(data, entityToSave);

                DB.Set <TEntity>().Update(entityToSave);
            }

            if (autoSave)
            {
                await DB.SaveChangesAsync();
            }

            return(entityToSave);
        }
Esempio n. 3
0
        public virtual async Task <TEntity> GetById(Guid id, bool disableTracking = true, Func <IQueryable <TEntity>, IIncludableQueryable <TEntity, object> > include = null)
        {
            var entityQuery = DB.Set <TEntity>().AsQueryable();

            if (disableTracking)
            {
                entityQuery = entityQuery.AsNoTracking();
            }

            // We assign all passed includes (if there are any)
            entityQuery = entityQuery.AssignIncludes(include);

            var entity = await entityQuery
                         .FirstOrDefaultAsync(x => x.Id == id)
            ;

            ProjectExceptions.ThrowNotFoundIfNull(entity);

            return(entity);
        }