public static IExpressionSpecification <T> Not <T>(this IExpressionSpecification <T> specification) { var expressionSpecificationNotOperator = new ExpressionSpecificationCombineNotOperator(); var expressionSpecification = expressionSpecificationNotOperator.Apply(specification); return(expressionSpecification); }
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); }
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); }
public TEntity[] Filter(IExpressionSpecification <TEntity> specification) { IQueryable <TEntity> entities = dbSet; IQueryable <TEntity> query = entities.Where(specification.ToExpression()); return(query.ToArray()); }
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); }
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()); }
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); }
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); }
// 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); }
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()); }
public async Task <Book> GetFirstAsync(IExpressionSpecification <Book> specification, CancellationToken cancellationToken = default) { var paginatedCollection = await GetAsync(specification, 0, 1, cancellationToken); return(paginatedCollection.Data.First()); }
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)); }
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)); }
public OrSpecification(IExpressionSpecification <T> left, IExpressionSpecification <T> right) : base(left, right) { }
public FolderSpecification(IExpressionSpecification <T> left, IExpressionSpecification <T> right) { this.left = left ?? throw new ArgumentNullException(nameof(left)); this.right = right ?? throw new ArgumentNullException(nameof(right)); }
public CompositeSpecification <T> Or(IExpressionSpecification <T> other) => new OrSpecification(this, other);
public CompositeSpecification <T> And(IExpressionSpecification <T> other) => new AndSpecification(this, other);