The relationship between Model, View and Controller make me confused.
This topic shows an arrow form View to Controller, an arrow from Controller to Model and an arrow from Model to View: http://www.codeproject.com/Tips/31292/MVC-v-s-MVP-How-Common-and-How-Different
However, this topic shows a dual-arrow between Model and View; a dual-arrow between View and Controller ; and an arrow from Controller to Model: http://www.codeproject.com/Articles/288928/Differences-between-MVC-and-MVP-for-Beginners
Finally, this topic shows an arrow from View to Model, an arrow from Controller to Model and an arrow from Controller to View: http://www.w3schools.com/aspnet/mvc_intro.asp
I have some questions:
- Which relationships are correct?
- Business Logic should be handled in Controller or Model? I've read somewhere that the business logic shouldn't be placed in Controller (ASP.Net MVC)
- In case the controller pass an object to a view, is this object belonged to Model?
- How does the view retrieve data directly from model? Does it have the reference directly to model or it interact with the model coming from Controller?
I find all of the images you're linking to confusing. This image (taken from Wikipedia) explaines it best.
-- Quoted from Patterns of Enterprise Application Architecture by Martin Fowler
Your questions
Typically the view fetches the necessary information from the model. When using passive views, objects (from the model) are passed from the controller. Important is that the view only reads from the model and never writes/updates it.The View observes and responds to changes in model. The model is the Domain Model rather than an individual recordset or entity.
Errata
How MVC is commonly used in the present time differs from the original MVC pattern as it was coined by Martin Fowler. He based this pattern in Smalltalk.
Another part of MVC is how the model, view and controller interact.
This is very different from MVC as made popular by Ruby on Rails, where the controller is responsible for preparing and loading the view.
Martin Fowler condenses MVC down to this
-- Quoted from GUI Architectures by Martin Fowler
There are lots of confusion with the Wikipedia's MVC image. The issue is that in UML
A->B
means that A is active and calls B. But wikipedists community is not pure technical and draws images with other confusing meanings of arrows. It looks nice and reasonable to have a full circle of arrows, no?No. Especially the
Model --updates--> View
relation is abomination. Technically it isView --reads--> Model
. If the Model were active, it would mean that the data objects and actions are dependent on other parts of the system, so it would not be reusable.The other nonsense is the
User --uses--> Controller
. Controller is invisible, users use only View, other parts are blackbox to them. The Controller is basically system of events implementations. The source of the events can be User's input or Model's data change, but they uses interfaces and it is the Controller which implements them. (It is called Inversion of Control and because of it some people confusingly draws the arrows in opposite direction.) These actions command Model and View to update, therefore the arrows point from the Controller. Nothing controls Controller. That's why it is called Controller: all control is aggregated into it. Active View is an exception: it can read Model without Controller's blessing if it needs to fill its selectbox for instance. But sometimes View's dependence on Model's interface is undesirable, so not all Views are designed active.So the correct image is
Or, if you want to incorporate User in the image, from the system's perspective it is an event source, not necessarily the only one:
Active Model scenario
Sometimes the Model can be also the source of events, i.e. if some account credit drops below some level, it could signal the View to show warning. But the Model should be independent to other part of the system and Observer design pattern is the way to implement it, see the simplified example:
Model
Model uses interface to let the View hook to its event of "account too low" or "account too high"
View
Some people would recommend to aggregate Observer rather than implement it. The inheritance is simpler and if the model defines all the observers in a way that their methods have unique names, we can inherit.
Controller
In the Controller part of the architecture we put together the user interface (View) and other event sources with Model (which may consist of more than one data sources). In our simplest case:
Note the
model.setAccountObserver(view)
- the model and view objects (as properties of the Controller) are coupled, but the Model and View classes are independent. This Dependency Injection pattern is the key to understand the Model - View relation.Now to your questions