public async Task <CosmosDocumentResponse <TDocument, TEntity> > GetBySpecificationAsync(ExpressionSpecification <TDocument> documentSpecification, string partitionKey = "", int pageNumber = 1, int pageSize = 100) { var cosmosDocumentResponse = new CosmosDocumentResponse <TDocument, TEntity>(); using var cosmosClient = new CosmosClient(CosmosDbConfiguration.Endpoint, CosmosDbConfiguration.AccessKey); var container = cosmosClient.GetContainer(CosmosDbConfiguration.DatabaseName, CosmosDbConfiguration.CollectionName); var queryRequestOptions = CosmosDbUtilities.SetQueryRequestOptions(partitionKey, pageSize); var documentQueryable = container.GetItemLinqQueryable <TDocument>(requestOptions: queryRequestOptions) .Where(documentSpecification.ToExpression()).Skip((pageNumber - 1) * pageSize).Take(pageSize); var documentIterator = documentQueryable.ToFeedIterator(); while (documentIterator.HasMoreResults) { var documentFeedResponse = await documentIterator.ReadNextAsync().ConfigureAwait(false); cosmosDocumentResponse = new CosmosDocumentResponse <TDocument, TEntity>(documentFeedResponse.StatusCode, documentFeedResponse.RequestCharge, documentFeedResponse.Resource.ToList(), _mapper); } return(cosmosDocumentResponse); }
/// <summary> /// How to use /// AddInclude(b => b.Items); with specification /// </summary> /// <param name="inputQuery"></param> /// <param name="specification"></param> /// <returns></returns> public static IQueryable <T> GetQuery(IQueryable <T> inputQuery, ExpressionSpecification <T> specification) { var query = inputQuery; // Add where condition first query = query.Where(specification.ToExpression()); // Includes all expression-based includes query = specification.Includes.Aggregate(query, (current, include) => current.Include(include)); // Include any string-based include statements query = specification.IncludeStrings.Aggregate(query, (current, include) => current.Include(include)); return(query); }
public async Task <bool> ExistsAsync(ExpressionSpecification <T> specification) { CheckExpression(specification); return(await DbSet.AnyAsync(specification.ToExpression())); }
public async Task <IReadOnlyList <T> > GetAllAsync(ExpressionSpecification <T> specification) { CheckExpression(specification); return(await DbSet.Where(specification.ToExpression()).ToListAsync()); }
public IEnumerable <TEntity> SelectPage(int page, int pageSize, out int totalCount) { totalCount = _repository.Select(_specification.ToExpression()).Count(); return(_repository.Select(_specification.ToExpression(), _orderBy, _includes, page, pageSize)); }