Browse Source

Merge pull request #243 from dotnet-architecture/groupby-specification-example

Adding a GroupBy specification example
main
Eric Fleming 7 years ago
committed by GitHub
parent
commit
b7d806dacd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      src/ApplicationCore/Interfaces/ISpecification.cs
  2. 8
      src/ApplicationCore/Specifications/BaseSpecification.cs
  3. 8
      src/Infrastructure/Data/SpecificationEvaluator.cs

1
src/ApplicationCore/Interfaces/ISpecification.cs

@ -11,6 +11,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
List<string> IncludeStrings { get; } List<string> IncludeStrings { get; }
Expression<Func<T, object>> OrderBy { get; } Expression<Func<T, object>> OrderBy { get; }
Expression<Func<T, object>> OrderByDescending { get; } Expression<Func<T, object>> OrderByDescending { get; }
Expression<Func<T, object>> GroupBy { get; }
int Take { get; } int Take { get; }
int Skip { get; } int Skip { get; }

8
src/ApplicationCore/Specifications/BaseSpecification.cs

@ -16,6 +16,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
public List<string> IncludeStrings { get; } = new List<string>(); public List<string> IncludeStrings { get; } = new List<string>();
public Expression<Func<T, object>> OrderBy { get; private set; } public Expression<Func<T, object>> OrderBy { get; private set; }
public Expression<Func<T, object>> OrderByDescending { 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 Take { get; private set; }
public int Skip { get; private set; } public int Skip { get; private set; }
@ -43,5 +44,12 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{ {
OrderByDescending = 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;
}
} }
} }

8
src/Infrastructure/Data/SpecificationEvaluator.cs

@ -1,10 +1,7 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
namespace Microsoft.eShopWeb.Infrastructure.Data namespace Microsoft.eShopWeb.Infrastructure.Data
{ {
@ -38,6 +35,11 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
query = query.OrderByDescending(specification.OrderByDescending); query = query.OrderByDescending(specification.OrderByDescending);
} }
if (specification.GroupBy != null)
{
query = query.GroupBy(specification.GroupBy).SelectMany(x => x);
}
// Apply paging if enabled // Apply paging if enabled
if (specification.isPagingEnabled) if (specification.isPagingEnabled)
{ {

Loading…
Cancel
Save