存储库是否可以返回 DDD 中的分页集合?



可以在域存储库中返回页面对象,例如UserRepository中的page 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; }
}
}

你搜索类似的东西吗?

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface UserRepository<User> extends PagingAndSortingRepository<User, Long> {
Page<User> findUsers(Pageable pageable);
}

最新更新