Merge branch 'master' into Removing-unused-directives
This commit is contained in:
@@ -20,6 +20,10 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
||||
{
|
||||
return SpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
||||
}
|
||||
|
||||
public virtual T GetById(int id)
|
||||
{
|
||||
@@ -42,44 +46,26 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
return _dbContext.Set<T>().AsEnumerable();
|
||||
}
|
||||
|
||||
public async Task<List<T>> ListAllAsync()
|
||||
public async Task<IReadOnlyList<T>> ListAllAsync()
|
||||
{
|
||||
return await _dbContext.Set<T>().ToListAsync();
|
||||
}
|
||||
|
||||
public IEnumerable<T> List(ISpecification<T> spec)
|
||||
{
|
||||
// fetch a Queryable that includes all expression-based includes
|
||||
var queryableResultWithIncludes = spec.Includes
|
||||
.Aggregate(_dbContext.Set<T>().AsQueryable(),
|
||||
(current, include) => current.Include(include));
|
||||
|
||||
// modify the IQueryable to include any string-based include statements
|
||||
var secondaryResult = spec.IncludeStrings
|
||||
.Aggregate(queryableResultWithIncludes,
|
||||
(current, include) => current.Include(include));
|
||||
|
||||
// return the result of the query using the specification's criteria expression
|
||||
return secondaryResult
|
||||
.Where(spec.Criteria)
|
||||
.AsEnumerable();
|
||||
return ApplySpecification(spec).AsEnumerable();
|
||||
}
|
||||
public async Task<List<T>> ListAsync(ISpecification<T> spec)
|
||||
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec)
|
||||
{
|
||||
// fetch a Queryable that includes all expression-based includes
|
||||
var queryableResultWithIncludes = spec.Includes
|
||||
.Aggregate(_dbContext.Set<T>().AsQueryable(),
|
||||
(current, include) => current.Include(include));
|
||||
|
||||
// modify the IQueryable to include any string-based include statements
|
||||
var secondaryResult = spec.IncludeStrings
|
||||
.Aggregate(queryableResultWithIncludes,
|
||||
(current, include) => current.Include(include));
|
||||
|
||||
// return the result of the query using the specification's criteria expression
|
||||
return await secondaryResult
|
||||
.Where(spec.Criteria)
|
||||
.ToListAsync();
|
||||
return await ApplySpecification(spec).ToListAsync();
|
||||
}
|
||||
public int Count(ISpecification<T> spec)
|
||||
{
|
||||
return ApplySpecification(spec).Count();
|
||||
}
|
||||
public async Task<int> CountAsync(ISpecification<T> spec)
|
||||
{
|
||||
return await ApplySpecification(spec).CountAsync();
|
||||
}
|
||||
|
||||
public T Add(T entity)
|
||||
|
||||
44
src/Infrastructure/Data/SpecificationEvaluator.cs
Normal file
44
src/Infrastructure/Data/SpecificationEvaluator.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
{
|
||||
public class SpecificationEvaluator<T> where T : BaseEntity
|
||||
{
|
||||
public static IQueryable<T> GetQuery(IQueryable<T> inputQuery, ISpecification<T> specification)
|
||||
{
|
||||
var query = inputQuery;
|
||||
|
||||
// modify the IQueryable using the specification's criteria expression
|
||||
if (specification.Criteria != null)
|
||||
query = query.Where(specification.Criteria);
|
||||
|
||||
// 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));
|
||||
|
||||
// Apply ordering if expressions are set
|
||||
if (specification.OrderBy != null)
|
||||
query = query.OrderBy(specification.OrderBy);
|
||||
else if (specification.OrderByDescending != null)
|
||||
query = query.OrderByDescending(specification.OrderByDescending);
|
||||
|
||||
// Apply paging if enabled
|
||||
if (specification.isPagingEnabled)
|
||||
{
|
||||
query = query.Skip(specification.Skip)
|
||||
.Take(specification.Take);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user