N
N
Nikita2020-10-05 21:03:41
PHP
Nikita, 2020-10-05 21:03:41

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

2 answer(s)
D
Daria Motorina, 2020-10-05
@somebug

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

A
Anton Shamanov, 2020-10-05
@SilenceOfWinter

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 question

Ask a Question

731 491 924 answers to any question