可以返回域存储库中的页面对象,例如 UserRepository 中的页面 findUsers(int page, int limit) 方法。
最佳答案
一般来说,您应该避免查询您的域模型。聚合通常不适合查询,因为它们通常代表某种形式的图。您的相当多的数据也可能被封装并公开,以便查询可能并不理想。
我建议将查询和分页转移到显式查询层,该查询层以有意义的形式返回数据。我倾向于使用 I{AggregateName}Query
但您可以将其命名为任何有意义的名称。我可以有以下方法:
public interface ICustomerQuery
{
IEnumerable<Query.Customer> Search(Query.Customer.Specification specification);
int Count(Query.Customer.Specification specification);
Page<Query.Customer> Search(Query.Customer.Specification specification, int page);
}
在这里,我的主命名空间中有一个命名空间Query
,它可能包含读取模型,这些模型是纯数据传输对象(数据容器),仅包含形状而不包含行为。 Customer
域聚合很可能包含一个联系人和地址列表,我不想仅仅为了在列表中显示它们而对其进行合并。另一方面,Query.Customer
读取模型是一个相当平坦的事情,尽管您可能选择为某些获取策略包含更复杂的内容。例如:
namespace SomeProduct.Query
{
public class Customer
{
Guid Id { get; set; }
string Name { get; set; }
string PrimaryAddress { get; set; }
}
}
关于java - 存储库可以返回 DDD 中的分页集合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58490787/