In Domain Driven Design, should the specification pattern for paging/pagination exist on the Repository level or Application level, or another layer? Trying to see what Eric Evans author of DDD stated. Then I can implement the code in correct layer. Sample code below.
Paging meaning, example have 100 products, and want to display 5 for each 20 pages.
Example 1: https://deviq.com/specification-pattern/
// https://github.com/dotnet-architecture/eShopOnWeb
public IEnumerable<T> List(ISpecification<T> spec)
{
// fetch a Queryable that includes all expression-based includes
var queryableResultWithIncludes = spec.Includes
.Aggregate(_dbContext.Set<T>().AsQueryable(),
(current, include) => current.Include(include));
// modify the IQueryable to include any string-based include statements
var secondaryResult = spec.IncludeStrings
.Aggregate(queryableResultWithIncludes,
(current, include) => current.Include(include));
// return the result of the query using the specification's criteria expression
return secondaryResult
.Where(spec.Criteria)
.AsEnumerable();
}
Example 2: https://thinkbeforecoding.com/post/2009/01/19/Repositories-and-IQueryable-the-paging-case
public interface IPaged<T> : IEnumerable<T>
{
int Count { get;}
IEnumerable<T> GetRange(int index, int count);
}
public class Paged<T> : IPaged<T>
{
private readonly IQueryable<T> source;
public Paged(IQueryable<T> source)
{
this.source = source;
}
public IEnumerator<T> GetEnumerator()
{
return source.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count
{
get { return source.Count(); }
}
public IEnumerable<T> GetRange(int index, int count)
{
return source.Skip(index).Take(count);
}
}
Code for Paging/Pagination