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,63 +0,0 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Helpers.Query;
|
||||
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
||||
{
|
||||
public abstract class BaseSpecification<T> : ISpecification<T>
|
||||
{
|
||||
protected BaseSpecification(Expression<Func<T, bool>> criteria)
|
||||
{
|
||||
Criteria = criteria;
|
||||
}
|
||||
public Expression<Func<T, bool>> Criteria { get; }
|
||||
public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();
|
||||
public List<string> IncludeStrings { get; } = new List<string>();
|
||||
public Expression<Func<T, object>> OrderBy { get; private set; }
|
||||
public Expression<Func<T, object>> OrderByDescending { get; private set; }
|
||||
public Expression<Func<T, object>> GroupBy { get; private set; }
|
||||
|
||||
public int Take { get; private set; }
|
||||
public int Skip { get; private set; }
|
||||
public bool IsPagingEnabled { get; private set; } = false;
|
||||
|
||||
protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
|
||||
{
|
||||
Includes.Add(includeExpression);
|
||||
}
|
||||
|
||||
protected virtual void AddIncludes<TProperty>(Func<IncludeAggregator<T>, IIncludeQuery<T, TProperty>> includeGenerator)
|
||||
{
|
||||
var includeQuery = includeGenerator(new IncludeAggregator<T>());
|
||||
IncludeStrings.AddRange(includeQuery.Paths);
|
||||
}
|
||||
|
||||
protected virtual void AddInclude(string includeString)
|
||||
{
|
||||
IncludeStrings.Add(includeString);
|
||||
}
|
||||
protected virtual void ApplyPaging(int skip, int take)
|
||||
{
|
||||
Skip = skip;
|
||||
Take = take;
|
||||
IsPagingEnabled = true;
|
||||
}
|
||||
protected virtual void ApplyOrderBy(Expression<Func<T, object>> orderByExpression)
|
||||
{
|
||||
OrderBy = orderByExpression;
|
||||
}
|
||||
protected virtual void ApplyOrderByDescending(Expression<Func<T, object>> orderByDescendingExpression)
|
||||
{
|
||||
OrderByDescending = orderByDescendingExpression;
|
||||
}
|
||||
|
||||
//Not used anywhere at the moment, but someone requested an example of setting this up.
|
||||
protected virtual void ApplyGroupBy(Expression<Func<T, object>> groupByExpression)
|
||||
{
|
||||
GroupBy = groupByExpression;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Ardalis.Specification;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Ardalis.Specification;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Ardalis.Specification;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
@@ -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
|
||||
{
|
||||
@@ -8,7 +9,8 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user