Answer the question
In order to leave comments, you need to log in
Does this code violate the Dependency Inversion principle?
Will creating an entity object in a controller be a DI violation?
Example from symfony demo:
https://github.com/symfony/demo/blob/e5e5a8fff0483...
public function new(Request $request): Response
{
$post = new Post();
$post->setAuthor($this->getUser());
...
Answer the question
In order to leave comments, you need to log in
There is no violation here, because there is no additional business logic in the form of different post subtypes, so you can limit yourself to creating a post locally, within the controller action. A DI violation is present when it is clearly seen that the classes depend on the implementation where it is time to create an abstraction
won't, but it's better to use the abstract factory
ps new is reserved by php and can't be a method name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question