我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 –