From 468df47c221c7caa600f122a653db9e78b3ea017 Mon Sep 17 00:00:00 2001 From: Eric Fleming Date: Sat, 20 Apr 2019 21:49:57 -0400 Subject: [PATCH 1/2] Adding a GroupBy specification example --- src/ApplicationCore/Interfaces/ISpecification.cs | 1 + src/ApplicationCore/Specifications/BaseSpecification.cs | 7 +++++++ .../Specifications/CatalogFilterPaginatedSpecification.cs | 1 + src/Infrastructure/Data/SpecificationEvaluator.cs | 8 +++++--- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ApplicationCore/Interfaces/ISpecification.cs b/src/ApplicationCore/Interfaces/ISpecification.cs index a61420f..f67414a 100644 --- a/src/ApplicationCore/Interfaces/ISpecification.cs +++ b/src/ApplicationCore/Interfaces/ISpecification.cs @@ -11,6 +11,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces List IncludeStrings { get; } Expression> OrderBy { get; } Expression> OrderByDescending { get; } + Expression> GroupBy { get; } int Take { get; } int Skip { get; } diff --git a/src/ApplicationCore/Specifications/BaseSpecification.cs b/src/ApplicationCore/Specifications/BaseSpecification.cs index 1017f3d..bcf1284 100644 --- a/src/ApplicationCore/Specifications/BaseSpecification.cs +++ b/src/ApplicationCore/Specifications/BaseSpecification.cs @@ -16,6 +16,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications public List IncludeStrings { get; } = new List(); public Expression> OrderBy { get; private set; } public Expression> OrderByDescending { get; private set; } + public Expression> GroupBy { get; private set; } public int Take { get; private set; } public int Skip { get; private set; } @@ -43,5 +44,11 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications { OrderByDescending = orderByDescendingExpression; } + + protected virtual void ApplyGroupBy(Expression> groupByExpression) + { + GroupBy = groupByExpression; + } + } } diff --git a/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs b/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs index f24b8c6..562492d 100644 --- a/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs +++ b/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs @@ -9,6 +9,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications (!typeId.HasValue || i.CatalogTypeId == typeId)) { ApplyPaging(skip, take); + ApplyGroupBy(i => new { i.CatalogType }); } } } diff --git a/src/Infrastructure/Data/SpecificationEvaluator.cs b/src/Infrastructure/Data/SpecificationEvaluator.cs index 88910d2..f029066 100644 --- a/src/Infrastructure/Data/SpecificationEvaluator.cs +++ b/src/Infrastructure/Data/SpecificationEvaluator.cs @@ -1,10 +1,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Interfaces; -using System; -using System.Collections.Generic; using System.Linq; -using System.Text; namespace Microsoft.eShopWeb.Infrastructure.Data { @@ -38,6 +35,11 @@ namespace Microsoft.eShopWeb.Infrastructure.Data query = query.OrderByDescending(specification.OrderByDescending); } + if (specification.GroupBy != null) + { + query = query.GroupBy(specification.GroupBy).SelectMany(x => x); + } + // Apply paging if enabled if (specification.isPagingEnabled) { From 0d606d18572a405c76f2e2a05456bdc89a5d4f81 Mon Sep 17 00:00:00 2001 From: Eric Fleming Date: Wed, 1 May 2019 19:41:22 -0400 Subject: [PATCH 2/2] Removing the apply in the CatalogFilterPaginationSpecification - This is so I can commit this to the repo as an example. --- src/ApplicationCore/Specifications/BaseSpecification.cs | 1 + .../Specifications/CatalogFilterPaginatedSpecification.cs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ApplicationCore/Specifications/BaseSpecification.cs b/src/ApplicationCore/Specifications/BaseSpecification.cs index bcf1284..f87b8d5 100644 --- a/src/ApplicationCore/Specifications/BaseSpecification.cs +++ b/src/ApplicationCore/Specifications/BaseSpecification.cs @@ -45,6 +45,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications OrderByDescending = orderByDescendingExpression; } + //Not used anywhere at the moment, but someone requested an example of setting this up. protected virtual void ApplyGroupBy(Expression> groupByExpression) { GroupBy = groupByExpression; diff --git a/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs b/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs index 562492d..f24b8c6 100644 --- a/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs +++ b/src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs @@ -9,7 +9,6 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications (!typeId.HasValue || i.CatalogTypeId == typeId)) { ApplyPaging(skip, take); - ApplyGroupBy(i => new { i.CatalogType }); } } }