I have a problem convincing my team mates why we shouldn't use (directly reference) the controller in the view when developing components in the spirit of MVC. I have invoked decoupling and natural intuition, but still those arguments didn't get through. They say, in their defense, that this is a normal compromise.

What arguments are convincing? Or they are right? How can the practice of using the controller in the view could affect a project on the long run?

  • 2
    can you give a more specific idea of how they want to use the controller in the view? – GSto May 24 '11 at 18:51
  • Get your boss to agree with you and have him tell the developers they have to do it – briddums Apr 2 '12 at 17:32

The View can have an indirect association to the Controller. Is it mandated? No, but it is certainly acceptable. When you state reference; I am assuming you are referring to a direct association between the View and the Controller which is typically avoided since it couples the View to a given Controller.

The idea is the isolation of concerns (maintainability) and the facilitation for better testing across the product. If other practices are being followed, the benefit to this separation should surface immediately; if not it may be the first step in this debate (CI, TDD, etc...).

If this is nothing more then an architecture pissing match; everyone is right and wrong and you all should move forward in the most maintainable way possible.

  • 4
    The last sentence is gold. – Tamás Szelei May 24 '11 at 19:52
  • 2
    "which is typically avoided since it couples the View to a given Controller" <-- THIS. Often it makes sense for a view to be used in multiple places. For instance, imagine an order of goods. You may use the same view for the user to review the order, as for the supplier to act on an order. Obviously, the controller which allows a supplier to authorise an order should not be referenced by this view, and neither should the controller from where the order is being placed. – Vincent McNabb Aug 8 '11 at 7:40

I tend to use it to restrict the view to only presentation logic.

A rule of thumb is that in cases where you find you need to access the concerns of one layer of an MVC app from a different layer, it is usually due to a deficiency in one of the other layers.

If you find yourself needing to access the controller from the view, you might want to look at the model and view and ask if it needs some additional data to track state, handle missing or ambiguous data or forward to an error or exception state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.