DDD について学習しようとしていますが、エンティティとリポジトリについて理解できないことがあります。
ここでの他の質問から、リポジトリをエンティティに挿入するのは悪い習慣であることに気付きました。しかし、オブジェクトを作成しているときにリポジトリの注入を避けるにはどうすればよいですか?
簡単な状況を見てみましょう - イベントとイベント アプリケーション。これは簡単に思えます。
$event->add($application);
$eventRepository->save($event);
$application はエンティティであると信じているので、$applicationRepository が必要だと思います。
Event エンティティを保存するために $applicationRepository を $eventRepository に注入する必要があるということですか? お気に入り
class eventRepository {
...
public function save(Event $event) {
...
foreach ($event->applications as $app) {
$this->applicationRepository->save($app);
}
...
}
}
私の頭に浮かんだ別の解決策はこれです:
$eventService->addAplication($event, $application);
class $eventService {
...
public function addApplication(Event $event, Application $app) {
// simple example of validation, something like $event->isAplyable()
if ($event->capacity > count($event->applications)) {
$this->applicationRepository->save($app);
$event->addApplication($app);
}
}
}
ある方法は他の方法よりも優れていますか? それとも私はそれを完全に台無しにしましたか?