Answer the question
In order to leave comments, you need to log in
When to do DependencyInjection and when to just dispatch Event?
Example, the user has registered, instead of an additional injection we make an event, $this->dispathc infoActionLog(new UserLogged($id))
There we log the action, and in the Lsitener of the event, after we have completed the action of writing the event to the log file, instead of immediately performing another action in the same file, namely, send a notification to the user , make a second event that will send an email to the user.
So we only test the events that happened and not the code itself, are there any events? All OK.
Not tivents in test response? We catch exceptions, everything is ok.
If we make a dependency, we make the module more rigid, related to the email module, which is not very good. Same thing with the logging module. With events it turns out that each file is responsible for 1 action, and is in no way connected with external modules, the dependencies are minimal.
Answer the question
In order to leave comments, you need to log in
Dependency Injection - when it is necessary to inject something into an object without which the object cannot work
Events - when it is necessary to connect end-to-end functionality to the object (i.e. one that does not affect the tasks solved by the object and in which there is no urgent need)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question