public static IExpressionSpecification <T> Not <T>(this IExpressionSpecification <T> specification)
        {
            var expressionSpecificationNotOperator = new ExpressionSpecificationCombineNotOperator();
            var expressionSpecification            = expressionSpecificationNotOperator.Apply(specification);

            return(expressionSpecification);
        }
Esempio n. 2
0
        public static IExpressionSpecification <T> And <T>(this IExpressionSpecification <T> specificationLeft, IExpressionSpecification <T> specificationRight)
        {
            var specificationAndOperator = new ExpressionSpecificationCombineAndOperator();
            var expressionSpecification  = specificationAndOperator.Combine(specificationLeft, specificationRight);

            return(expressionSpecification);
        }
        public async Task <PaginatedCollection <StockResponse> > Handle(QueryStockCommand request, CancellationToken cancellationToken)
        {
            IExpressionSpecification <Stock> specification = ExpressionSpecification <Stock> .Default;

            if (!string.IsNullOrEmpty(request.ProductId))
            {
                specification = specification.And(new ProductIdIs(request.ProductId));
            }

            if (request.InStock.HasValue)
            {
                if (request.InStock.Value)
                {
                    specification = specification.And(new StockCountGreaterThan(0));
                }
                else
                {
                    specification = specification.And(new StockCountLessThan(1));
                }
            }

            IStockRepository            stockRepository     = _stockDbContext.StockRepository;
            PaginatedCollection <Stock> paginatedCollection = await stockRepository.GetAsync(specification, request.Offset, request.Limit, cancellationToken);

            PaginatedCollection <StockResponse> result = new PaginatedCollection <StockResponse>(paginatedCollection.TotalCount,
                                                                                                 paginatedCollection.Data
                                                                                                 .Select(stock => new StockResponse(stock.ProductId,
                                                                                                                                    stock.AvailableStock))
                                                                                                 .ToList());

            return(result);
        }
Esempio n. 4
0
        public async Task <Stock> GetFirstAsync(IExpressionSpecification <Stock> specification, CancellationToken cancellationToken)
        {
            var paginatedCollection = await GetAsync(specification, 0, 1, OrderBy <Stock> .Asc(stock => stock.CreatedOn), cancellationToken);

            var stock = paginatedCollection.Data.First();

            return(stock);
        }
Esempio n. 5
0
        public TEntity[] Filter(IExpressionSpecification <TEntity> specification)
        {
            IQueryable <TEntity> entities = dbSet;

            IQueryable <TEntity> query = entities.Where(specification.ToExpression());

            return(query.ToArray());
        }
Esempio n. 6
0
 private IQueryable <TEntity> ApplySpecification <TModel>(IExpressionSpecification <TEntity, TModel> specification)
 {
     return(_appDbContext
            .Set <TEntity>()
            .AsNoTracking()
            .Includes(specification.Includes)
            .Where(specification.Criteria)
            .OrderBys(specification.SortingInstructions));
 }
        public IExpressionSpecification <TModel> Apply <TModel>(IExpressionSpecification <TModel> specification)
        {
            var candidateExpr = specification.Expression.Parameters[0];
            var body          = Expression.Not(specification.Expression.Body);

            var resultExpression      = Expression.Lambda <Func <TModel, bool> >(body, candidateExpr);
            var combinedSpecification = new DynamicExpressionSpecification <TModel>(resultExpression);

            return(combinedSpecification);
        }
Esempio n. 8
0
        public TEntity[] Filter(IExpressionSpecification <TEntity> specification)
        {
            IQueryable <TEntity> entities = set;

            if (specification.Includes.Length > 0)
            {
                foreach (string include in specification.Includes)
                {
                    entities = entities.Include(include);
                }
            }

            IQueryable <TEntity> query = entities.Where(specification.ToExpression());

            return(query.ToArray());
        }
Esempio n. 9
0
        public async Task <PaginatedCollection <Stock> > GetAsync(IExpressionSpecification <Stock> specification, int offset, int limit, OrderBy <Stock> orderBy, CancellationToken cancellationToken)
        {
            IQueryable <Stock> stocks = _appDbContext.Set <Stock>()
                                        .Where(specification.Expression);

            stocks = orderBy.Apply(stocks);

            (int totalCount, List <Stock> stockList)
                = await stocks.PaginatedQueryAsync(offset, limit, cancellationToken);


            if (!stockList.Any())
            {
                throw new StockNotFoundException();
            }

            var paginatedCollection = new PaginatedCollection <Stock>(totalCount, stockList);

            return(paginatedCollection);
        }
Esempio n. 10
0
        public async Task <PaginatedCollection <Order> > GetAsync(IExpressionSpecification <Order> specification, int offset, int limit, OrderBy <Order> orderBy, CancellationToken cancellationToken = default)
        {
            IQueryable <Order> books = _appDbContext.Set <Order>()
                                       .Include(order => order.OrderLines)
                                       .Where(specification.Expression);

            books = orderBy.Apply(books);

            (int totalCount, List <Order> orderList)
                = await books.PaginatedQueryAsync(offset, limit, cancellationToken);


            if (!orderList.Any())
            {
                throw new OrderNotFoundException();
            }

            var paginatedCollection = new PaginatedCollection <Order>(totalCount, orderList);

            return(paginatedCollection);
        }
Esempio n. 11
0
        // https://stackoverflow.com/questions/457316/combining-two-expressions-expressionfunct-bool
        public IExpressionSpecification <TModel> Combine <TModel>(IExpressionSpecification <TModel> left, IExpressionSpecification <TModel> right)
        {
            Expression <Func <TModel, bool> > resultExpression;
            var param = left.Expression.Parameters.First();

            if (ReferenceEquals(param, right.Expression.Parameters.First()))
            {
                resultExpression = Expression.Lambda <Func <TModel, bool> >(
                    Expression.AndAlso(left.Expression.Body, right.Expression.Body), param);
            }
            else
            {
                resultExpression = Expression.Lambda <Func <TModel, bool> >(
                    Expression.AndAlso(
                        left.Expression.Body,
                        Expression.Invoke(right.Expression, param)), param);
            }

            var combinedSpecification = new DynamicExpressionSpecification <TModel>(resultExpression);

            return(combinedSpecification);
        }
Esempio n. 12
0
 private static async Task <TModel> PerformFirstOrDefaultAsync <TModel>(IQueryable <TEntity> query, IExpressionSpecification <TEntity, TModel> specification)
 {
     if (specification is not IQueryExpressionSpecification <TEntity, TModel> querySpecification)
     {
         return(await query.Select(specification.Projection).FirstOrDefaultAsync());
     }
Esempio n. 13
0
        public async Task <Book> GetFirstAsync(IExpressionSpecification <Book> specification, CancellationToken cancellationToken = default)
        {
            var paginatedCollection = await GetAsync(specification, 0, 1, cancellationToken);

            return(paginatedCollection.Data.First());
        }
Esempio n. 14
0
 public Task <PaginatedCollection <Book> > GetAsync(IExpressionSpecification <Book> specification, int offset, int limit, CancellationToken cancellationToken = default)
 {
     return(GetAsync(specification, offset, limit, OrderBy <Book> .Asc(x => x.CreatedOn), cancellationToken));
 }
Esempio n. 15
0
 public Task <PaginatedCollection <Stock> > GetAsync(IExpressionSpecification <Stock> specification, int offset, int limit, CancellationToken cancellationToken)
 {
     return(GetAsync(specification, offset, limit, OrderBy <Stock> .Asc(stock => stock.CreatedOn), cancellationToken));
 }
Esempio n. 16
0
 public OrSpecification(IExpressionSpecification <T> left, IExpressionSpecification <T> right)
     : base(left, right)
 {
 }
Esempio n. 17
0
 public FolderSpecification(IExpressionSpecification <T> left, IExpressionSpecification <T> right)
 {
     this.left  = left ?? throw new ArgumentNullException(nameof(left));
     this.right = right ?? throw new ArgumentNullException(nameof(right));
 }
Esempio n. 18
0
 public CompositeSpecification <T> Or(IExpressionSpecification <T> other) => new OrSpecification(this, other);
Esempio n. 19
0
 public CompositeSpecification <T> And(IExpressionSpecification <T> other) => new AndSpecification(this, other);