我Zone
對象樹:DDD:是否可以將注入服務到實體
class Zone {
protected $parent;
public function __construct(Zone $parent) {
$this->parent = $parent;
}
}
有在區沒有children
也不descendants
屬性,因爲我想避免在領域模型中管理這些關係的痛苦。
相反,域名服務維護的關閉數據庫中的表,來映射區及其所有後代,在任何級別。現在
,我有可以分配一個或多個區域一個User
:
class User {
protected $zones;
public function assignZone(Zone $zone) {
$this->zones[] = $zone;
}
}
我的問題是,之前分配一個新的區域的用戶,我想檢查該區域不是已經通過它的後代明確地或隱含地分配的。
所以我想我的控制器瞬時注入到服務這種方法,進行必要的檢查:
class User {
protected $zones;
public function assignZone(Zone $newZone, ZoneService $zoneService) {
foreach ($this->zones as $zone) {
if ($service->zoneHasDescendant($zone, $newZone)) {
throw new Exception('The user is already assigned this zone');
}
}
$this->zones[] = $zone;
}
}
那是一個很好的做法,如果沒有,什麼是正確的選擇呢?
相關:http://stackoverflow.com/questions/4835046/why-not-use-an-ioc-container-to-resolve-dependencies-for-entities-business-object/4836790#4836790 –