Upgrade to use Specification 4.0.0 (#444)

This commit is contained in:
Steve Smith
2020-07-31 14:52:14 -04:00
committed by GitHub
parent e520126857
commit 754c845e9f
15 changed files with 63 additions and 52 deletions

View File

@@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="1.5.0" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="Ardalis.Specification" Version="4.0.0" />
<PackageReference Include="MediatR" Version="8.0.2" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />

View File

@@ -3,16 +3,20 @@ using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public sealed class BasketWithItemsSpecification : BaseSpecification<Basket>
public sealed class BasketWithItemsSpecification : Specification<Basket>
{
public BasketWithItemsSpecification(int basketId) : base(b => b.Id == basketId)
public BasketWithItemsSpecification(int basketId)
{
AddInclude(b => b.Items);
Query
.Where(b => b.Id == basketId)
.Include(b => b.Items);
}
public BasketWithItemsSpecification(string buyerId) : base(b => b.BuyerId == buyerId)
public BasketWithItemsSpecification(string buyerId)
{
AddInclude(b => b.Items);
Query
.Where(b => b.BuyerId == buyerId)
.Include(b => b.Items);
}
}
}

View File

@@ -3,13 +3,15 @@ using Microsoft.eShopWeb.ApplicationCore.Entities;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public class CatalogFilterPaginatedSpecification : BaseSpecification<CatalogItem>
public class CatalogFilterPaginatedSpecification : Specification<CatalogItem>
{
public CatalogFilterPaginatedSpecification(int skip, int take, int? brandId, int? typeId)
: base(i => (!brandId.HasValue || i.CatalogBrandId == brandId) &&
(!typeId.HasValue || i.CatalogTypeId == typeId))
: base()
{
ApplyPaging(skip, take);
Query
.Where(i => (!brandId.HasValue || i.CatalogBrandId == brandId) &&
(!typeId.HasValue || i.CatalogTypeId == typeId))
.Paginate(skip, take);
}
}
}

View File

@@ -3,13 +3,12 @@ using Microsoft.eShopWeb.ApplicationCore.Entities;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public class CatalogFilterSpecification : BaseSpecification<CatalogItem>
public class CatalogFilterSpecification : Specification<CatalogItem>
{
public CatalogFilterSpecification(int? brandId, int? typeId)
: base(i => (!brandId.HasValue || i.CatalogBrandId == brandId) &&
(!typeId.HasValue || i.CatalogTypeId == typeId))
{
Query.Where(i => (!brandId.HasValue || i.CatalogBrandId == brandId) &&
(!typeId.HasValue || i.CatalogTypeId == typeId));
}
}
}

View File

@@ -5,11 +5,11 @@ using System.Linq;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public class CatalogItemsSpecification : BaseSpecification<CatalogItem>
public class CatalogItemsSpecification : Specification<CatalogItem>
{
public CatalogItemsSpecification(params int[] ids) : base(c => ids.Contains(c.Id))
public CatalogItemsSpecification(params int[] ids)
{
Query.Where(c => ids.Contains(c.Id));
}
}
}

View File

@@ -1,16 +1,15 @@
using Ardalis.Specification;
using Ardalis.Specification.QueryExtensions.Include;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public class CustomerOrdersWithItemsSpecification : BaseSpecification<Order>
public class CustomerOrdersWithItemsSpecification : Specification<Order>
{
public CustomerOrdersWithItemsSpecification(string buyerId)
: base(o => o.BuyerId == buyerId)
{
AddIncludes(query => query.Include(o => o.OrderItems)
.ThenInclude(i => i.ItemOrdered));
Query.Where(o => o.BuyerId == buyerId)
.Include(o => o.OrderItems)
.ThenInclude(i => i.ItemOrdered);
}
}
}

View File

@@ -1,4 +1,5 @@
using Ardalis.Specification;
using Ardalis.Specification.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
@@ -34,13 +35,13 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec)
{
var specificationResult = await ApplySpecification(spec);
var specificationResult = ApplySpecification(spec);
return await specificationResult.ToListAsync();
}
public async Task<int> CountAsync(ISpecification<T> spec)
{
var specificationResult = await ApplySpecification(spec);
var specificationResult = ApplySpecification(spec);
return await specificationResult.CountAsync();
}
@@ -66,19 +67,20 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
public async Task<T> FirstAsync(ISpecification<T> spec)
{
var specificationResult = await ApplySpecification(spec);
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstAsync();
}
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec)
{
var specificationResult = await ApplySpecification(spec);
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstOrDefaultAsync();
}
private async Task<IQueryable<T>> ApplySpecification(ISpecification<T> spec)
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
{
return await EfSpecificationEvaluator<T>.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
var evaluator = new SpecificationEvaluator<T>();
return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
}
}
}

View File

@@ -7,7 +7,8 @@
<ItemGroup>
<PackageReference Include="Ardalis.EFCore.Extensions" Version="1.1.0" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="Ardalis.Specification" Version="4.0.0" />
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="4.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />

View File

@@ -47,7 +47,7 @@
Note that for demo purposes you don't need to register and can login with these credentials:
</p>
<p>
User: <b>demouser@microsoft.com</b>
User: <b>demouser@microsoft.com</b> OR <b>admin@microsoft.com</b>
</p>
<p>
Password: <b>Pass@word1</b>

View File

@@ -22,7 +22,7 @@
<ItemGroup>
<PackageReference Include="Ardalis.ApiEndpoints" Version="1.0.0" />
<PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="Ardalis.Specification" Version="4.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="MediatR" Version="8.0.2" />