Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        public async Task <bool> ExistsAsync(ExpressionSpecification <T> specification)
        {
            CheckExpression(specification);

            return(await DbSet.AnyAsync(specification.ToExpression()));
        }
Exemplo n.º 4
0
        public async Task <IReadOnlyList <T> > GetAllAsync(ExpressionSpecification <T> specification)
        {
            CheckExpression(specification);

            return(await DbSet.Where(specification.ToExpression()).ToListAsync());
        }
Exemplo n.º 5
0
        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));
        }