教义网站被关闭了,所以我在这里寻找信息:
什么应该包含原则2的实体:
仅setters
谢谢
发布于 2011-05-01 19:38:35
如果应用于实体本身,则某些域逻辑是可以的。例如,以下内容很好:
class myEntity {
// ...
/**
* @OneToMany(targetEntity="LineItem")
*/
protected $items;
public function equals($otherEntity){
//compare $this->lineItems and $otherEntity->lineItems, and return true if
//they are identical
}
/**
* More business logic internal to an entity.
*/
public function subtotal(){
$total = 0;
foreach($this->items as $i) $total += $i;
return $i;
}
}你不想在实体之外产生副作用的东西(或者它拥有的实体),数据持久性(实体永远不应该知道EntityManager,或者存储库等等)。
我的经验法则是几乎总是避免让我的实体有任何依赖项(相关实体类除外)。如果我突然需要一些复杂的东西,我知道是时候将逻辑从实体迁移到服务类了。
发布于 2011-05-02 00:49:10
实体应该包含业务逻辑。也就是说,逻辑应该只与实体本身和相关实体相关。正如@timdev已经说过的,实体应该是100%的持久性不可知论者。不应该使用EntityManager、存储库或服务;只使用其他实体。
您可能想看看我已经问过的similar question。
https://stackoverflow.com/questions/5850322
复制相似问题