我正在使用Entity和EntityRepository类在自己的ORM中工作
在任何ORM中,我探索将任何服务注入Entities都是一个坏主意。但是有一个问题:将服务注入到存储库中是个好主意吗?
有一个抽象的例子:
//$user is object of Entity class in ORM
//we need to get actual valid entities here
$relatedEntities = $user->getRelatedEntities();
//where getRelatedEntities() calls method of RelatedEntityRepository
RelatedEntityRepository {
private $injectedService;
public function getByUserId($userId){
if(!$this->injectedService->hasValidUserData($userId)){
$this->injectedService->doSomething($userId);
}
return $this->getFromDbByUserId($userId)
}
private function getFromDbByUserId($userId){
//returns data from DB
}
}