我正在使用实体类和实体类创建自己的ORM
在任何ORM中,我都探索过将任何服务注入实体是一个非常糟糕的主意。但有一个问题:将服务注入存储库是个好主意吗?
有一个抽象的例子:
//$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
}
}