Skip to content

Commit

Permalink
Switching to NuGet Ardalis.Specifications (dotnet-architecture#389)
Browse files Browse the repository at this point in the history
- updating to use Ardalis.Specifications package as it is maintained and has a more robust implementation
- Removing all custom specification implementation
- Updating unit tests
  • Loading branch information
efleming18 authored Jun 8, 2020
1 parent 1b5b12e commit 70a919e
Show file tree
Hide file tree
Showing 28 changed files with 45 additions and 310 deletions.
1 change: 1 addition & 0 deletions src/ApplicationCore/ApplicationCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="1.5.0" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
</ItemGroup>

Expand Down
26 changes: 0 additions & 26 deletions src/ApplicationCore/Helpers/Query/IncludeAggregator.cs

This file was deleted.

19 changes: 0 additions & 19 deletions src/ApplicationCore/Helpers/Query/IncludeQuery.cs

This file was deleted.

66 changes: 0 additions & 66 deletions src/ApplicationCore/Helpers/Query/IncludeQueryExtensions.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/ApplicationCore/Helpers/Query/IncludeVisitor.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/ApplicationCore/Interfaces/IAsyncRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand Down
16 changes: 0 additions & 16 deletions src/ApplicationCore/Interfaces/IIncludeQuery.cs

This file was deleted.

20 changes: 0 additions & 20 deletions src/ApplicationCore/Interfaces/ISpecification.cs

This file was deleted.

63 changes: 0 additions & 63 deletions src/ApplicationCore/Specifications/BaseSpecification.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;

namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public sealed class BasketWithItemsSpecification : BaseSpecification<Basket>
{
public BasketWithItemsSpecification(int basketId)
:base(b => b.Id == basketId)
public BasketWithItemsSpecification(int basketId) : base(b => b.Id == basketId)
{
AddInclude(b => b.Items);
}
public BasketWithItemsSpecification(string buyerId)
:base(b => b.BuyerId == buyerId)
public BasketWithItemsSpecification(string buyerId) : base(b => b.BuyerId == buyerId)
{
AddInclude(b => b.Items);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities;

namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities;

namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;
using System.Linq;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Helpers.Query;
using Ardalis.Specification;
using Ardalis.Specification.QueryExtensions.Include;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;

namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
Expand All @@ -8,7 +9,8 @@ public class CustomerOrdersWithItemsSpecification : BaseSpecification<Order>
public CustomerOrdersWithItemsSpecification(string buyerId)
: base(o => o.BuyerId == buyerId)
{
AddIncludes(query => query.Include(o => o.OrderItems).ThenInclude(i => i.ItemOrdered));
AddIncludes(query => query.Include(o => o.OrderItems)
.ThenInclude(i => i.ItemOrdered));
}
}
}
19 changes: 12 additions & 7 deletions src/Infrastructure/Data/EfRepository.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,12 +34,14 @@ public async Task<IReadOnlyList<T>> ListAllAsync()

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)
Expand All @@ -63,17 +66,19 @@ public async Task DeleteAsync(T entity)

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);
}
}
}
1 change: 1 addition & 0 deletions src/Infrastructure/Data/OrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using Ardalis.Specification;

namespace Microsoft.eShopWeb.Infrastructure.Data
{
Expand Down
Loading

0 comments on commit 70a919e

Please sign in to comment.