1

I have a class I call my Dispatcher and when its dispatch() method is run it instantiates the requested controller.

My AbstractController has a constructor like

public function __construct(RequestInterface $request, ResponseInterface $response, ViewFactory $viewFactory, ServiceFactory $serviceFactory)

As you can see my controllers have 4 dependencies.

At the moment when I instantiate my Dispatcher I inject the ViewFactory and ServiceFactory into its constructor and then when I run the dispatch() method I supply the Request and Response objects as arguments and I can then inject all four dependencies into my controller.

Would it be better to just supply all the controllers dependencies when calling the dispatch() method or supply them all in the constructor of the Dispatcher and then run a dispatch() method with no arguments or is there a better way overall?

tereško
  • 56,151
  • 24
  • 92
  • 147
ibanore
  • 1,430
  • 1
  • 12
  • 24

2 Answers2

2

You have 4 dependencies you want to inject in a new controller instance, 2 have a request/response life cycle and 2 have a longer life cycle (process?), before invoking some method on it. I would separate the creation of the controller from the invoking by creating a controller factory.

The controller factory would instantiate a controller using a method that receives only the request and reponse objects as parameters. The factory would be aware of the life cycle of the two longer lifed dependencies, perhaps share the same life cycle. The factory's user is then free to invoke any method they would like on the (abstract) controller returned. This is more flexible than the delegation pattern you propose. You could of course still use delegation, but then I would probably still have the Dispatcher only do dispatching and leave object creation to a factory.

Of course, there is no right answer with these kind of questions. It all depends on a lot of factors (requirements, size of codebase, the project, etc). Hope this helps though. Good luck!

Lodewijk Bogaards
  • 18,519
  • 2
  • 25
  • 49
2

Some "blah"

Here is a rule of thumb that I use when design API: if your class has 3+ dependencies, it is doing too much.

The dependencies that you pass in the constructor, should be mandatory for your instance to function. In your case, you have a controller there. Controllers repsonsibilities are as follows:

A controller can send commands to its associated view to change the view's presentation of the model. It can also send commands to the model to update the model's state.   source: wikipedia

Controllers should not be responsible for creation or rendering of the view instance.

Regarding the original question:

I am not sure what is the role of the Dispatcher instances, but they should not be dealing with creation logic itself. What you essentially end up with here is a mix of possible LoD violation and a definite SRP violation.

The creation logic should be segregate to factories and builders. This way would also let you decouple your Dispatcher instances from the footprint of controller's constructor. Currently, if you introduced a different subclass of controllers, with different set of dependencies, you would also need to change the Dispatcher implementation.

As for "how to make a factory", you can find a simplified example here.

Community
  • 1
  • 1
tereško
  • 56,151
  • 24
  • 92
  • 147
  • Thanks for the reply I have now created a `ControllerFactory` which I inject into my `Dispatcher` and it makes creating a controller easier and cleaner. Could you take a look at my question http://stackoverflow.com/questions/16123192/send-http-response-in-the-views-render-method which I posted yesterday if you have time please. – ibanore Apr 21 '13 at 10:03
  • +1. in MVC view has direct access to Model layer(Service), so that it handles presentation logic. *Controller can also send commands to the model to update the model's state* – Yang Apr 21 '13 at 10:09
  • "Change model" state sounds as a maze – Yang Apr 21 '13 at 10:11
  • I'm no expert in this area but I think when you invoke a controllers method it generally does not return a value. The controller will extract GET/POST data from the request and send it to the Service Layer to update the model. – ibanore Apr 21 '13 at 10:19
  • @David How a service could be a layer? What is a model then? – Yang Apr 21 '13 at 10:37
  • Have a good look at this http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000 – ibanore Apr 21 '13 at 10:42