douchuanchai2793 2012-11-06 15:54
浏览 96

实现将业务逻辑排序/过滤到DDD聚合中

I am fairly new to DDD and for me I search in practical terms to a good function for an aggregate. I have a long list of users (User) and a create an aggregate (UserAggregate) for that.

Now in my view I might display users based on different criteria. Let's assume here I don't sort the list in my database query, it is OK to have sorting in an aggregate? I cannot come up with another part of the application where I can do this properly, but I might misunderstand the function of an aggregate.

$users = new UserAggregate(array(
 $user1, $user2, $user3, $user4
));

foreach ($users->sortBySurname(), $user) {
 //
}

foreach ($users->sortByLastLoggedIn(), $user) {
 //
}

The same holds for filtering by the way. When I read about DDD aggregates, I immediately thought of the Doctrine\Common\Collections\Collection (link). That sort-of looks like an aggregate to me. So DDD experts, please enlighten me :-)

Update

The only way I was thinking btw was making aggregates rather immutual and have helpers to create new aggregates. But this seems not the way to go:

$aggregate = new UserAggregate(array($user1, $user2));
$helper    = new FilterSomething;
$aggregate = $helper->filter($aggregate);

The second aggregate is a new instance, so the helper looks like this:

class FilterSomething
{
  public function filter(Aggregate $aggregate)
  {
    $items = $aggregate->getItems();
    $items = $this->doFilter($items);

    return new Aggregate($items);
  }
}
  • 写回答

1条回答 默认 最新

  • drws65968272 2012-11-07 06:12
    关注

    A root aggregate is not a repository. A root aggregate represents a single user and all of it's domain specific information.

    That sorting and filtering should be applied in a repository.

    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败