Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I work with ASP.NET MVC (and other web-based MVC implementations) quite often, but this is something I've never been sure of: Should controller and view communicate?

Of course the controller should be choosing which view to use, but what I is mean should the controller be passing data to the view? In my opinion, if the view is expecting data from the controller, then they're effectively tied together as a (controller, view) pair. Instead, I usually have the view communicate with the model itself and be independent of any controller.

Do I have the right approach, or is this a case of there being no one correct answer? Does the answer change when working in the web versus other environments? Does the answer change when you have the concept of a strongly-typed view (like in ASP.NET MVC) or not?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

The controller prepares data which will further be passed to the view for rendering / displaying. It also accepts user input data through a publish-subscribe mechanism or similar. Check out the first diagram on Wikipedia or Martin Fowler's website for more information about MVC.

if the view is expecting data from the controller, then they're effectively tied together as a (controller, view) pair.

While a view generally accepts data, in most MVC frameworks, it does not depend on specific controllers. Exceptions are, for instance, the JavaServer Faces family. Generally speaking, frameworks like Rails, Django or Spring MVC allow you to decouple views from controllers by passing data (the context, commonly a map/dictionary/bag) to a view (where a view is an implementation of the template view pattern).

Does the answer change when you have the concept of a strongly-typed view (like in ASP.NET MVC) or not?

Whether or not your programming language is strongly-typed has no influence on the way your are organizing your application.

share|improve this answer
    
What kind of data is being prepared and passed across? Take a simple example: showing an article by it's ID. Is it the ID that is passed after verification (it might not point to an article), or does the controller get the article from the database and pass that across? –  AndyBursh Feb 11 '12 at 19:06
    
If you only pass the ID is your view not doing more work than rendering? It would have to retrieve data which would probably not be in the spirit of the pattern. –  Rig Feb 11 '12 at 19:58
add comment

The question you are raising is discussed in my team from time to time. We argue about two approaches, which both have their cons and pros.

The first, argues that the controller may update the view by the following pattern. It listens to both GUI and model events. When a GUI event occurs, it executes the required action in the model, which in turn fires and event. Now the controller is usually updating the view with the required data.

The second approach, argues that the view itself is listening to the model events and updates itself with the data which is either attached to the event or by querying the model.

In the first approach you have more power to the controller which is really controlling everything which is going on your application. The power to decide in which way the view should be updated according to what event is in his hands and this way you are keeping your view pure. However, as you said, this way you have your view and controller coupled.

In the second you are decoupling them but your view is actually controlling itself in some manner.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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