在领域驱动设计中,分页/分页的规范模式应该存在于存储层、应用程序层还是其他层?想看看埃里克·埃文斯( Evans )的DDD作者说了些什么。然后,我可以在正确的层实现代码。下面是示例代码。
分页意思是,示例有100个产品,并希望每20页显示5个。
Example 1: https://deviq.com/specification-pattern/
// https://github.com/dotnet-architecture/eShopOnWeb
public IEnumerable List(ISpecification spec)
{
// fetch a Queryable that includes all expression-based includes
var queryableResultWithIncludes = spec.Includes
.Aggregate(_dbContext.Set().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 : IEnumerable
{
int Count { get;}
IEnumerable GetRange(int index, int count);
}
public class Paged : IPaged
{
private readonly IQueryable source;
public Paged(IQueryable source)
{
this.source = source;
}
public IEnumerator GetEnumerator()
{
return source.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count
{
get { return source.Count(); }
}
public IEnumerable GetRange(int index, int count)
{
return source.Skip(index).Take(count);
}
}寻呼/分页代码
发布于 2019-08-21 16:19:17
Paging是一个实现细节。
DDD主要关注体系结构设计;对于如何处理分页问题,它没有什么可说的,因为处理诸如纸张大小或屏幕长度这样的问题并不是业务领域的一部分,尽管它可以是设计过程的一部分。
你应该把你的寻呼放哪里?对你最方便的地方。
我看到很多文章都谈到将此责任放在存储库中,使用规范模式或其他方式过滤记录。好吧,如果你想那样工作的话。就我个人而言,我认为分页是一个显示问题,所以我希望看到它更接近UI或打印操作,也许是在ViewModel或服务层。
让存储库处理分页有其好处。如果设计正确,则可能会减少正在检索的数据量,因为分页机制将只检索每个页所需的数据,而不会检索从未打印或显示的页的数据。
https://softwareengineering.stackexchange.com/questions/396308
复制相似问题