Switching to NuGet Ardalis.Specifications (#389)
- updating to use Ardalis.Specifications package as it is maintained and has a more robust implementation - Removing all custom specification implementation - Updating unit tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Ardalis.Specification;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
@@ -33,12 +34,14 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
|
||||
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec)
|
||||
{
|
||||
return await ApplySpecification(spec).ToListAsync();
|
||||
var specificationResult = await ApplySpecification(spec);
|
||||
return await specificationResult.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<int> CountAsync(ISpecification<T> spec)
|
||||
{
|
||||
return await ApplySpecification(spec).CountAsync();
|
||||
var specificationResult = await ApplySpecification(spec);
|
||||
return await specificationResult.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<T> AddAsync(T entity)
|
||||
@@ -63,17 +66,19 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
|
||||
public async Task<T> FirstAsync(ISpecification<T> spec)
|
||||
{
|
||||
return await ApplySpecification(spec).FirstAsync();
|
||||
var specificationResult = await ApplySpecification(spec);
|
||||
return await specificationResult.FirstAsync();
|
||||
}
|
||||
|
||||
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec)
|
||||
{
|
||||
return await ApplySpecification(spec).FirstOrDefaultAsync();
|
||||
var specificationResult = await ApplySpecification(spec);
|
||||
return await specificationResult.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
||||
private async Task<IQueryable<T>> ApplySpecification(ISpecification<T> spec)
|
||||
{
|
||||
return SpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
||||
return await EfSpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
using Ardalis.Specification;
|
||||
|
||||
namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
{
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using System.Linq;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (specification.GroupBy != null)
|
||||
{
|
||||
query = query.GroupBy(specification.GroupBy).SelectMany(x => x);
|
||||
}
|
||||
|
||||
// Apply paging if enabled
|
||||
if (specification.IsPagingEnabled)
|
||||
{
|
||||
query = query.Skip(specification.Skip)
|
||||
.Take(specification.Take);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Ardalis.EFCore.Extensions" Version="1.1.0" />
|
||||
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />
|
||||
|
||||
Reference in New Issue
Block a user