I read a lots of articles about MVC architecture, but I'm still confused.
Diagram 1
Diagram 2
Diagram 3
MVC can be understood by thinking of responsibilities:
The View is not allowed to change the state of the model directly - only through the Controller. The view can still have some direct access to the Model although only for viewing (or by having a copy that isn't the official Model).
The Model should live in its own universe and not have any reference to controllers or to views.
The Controller controls the state and access to the Model.
Definitely not Diagram 3! Diagram 1 is OK. I think the best is basically Diagram 2 with an arrow from Controller to View.
Assuming you are asking in the context of web apps, here is what I think a good MVC flow looks like:
When a web request comes, it is one of 2 types.
Type A - this is a simple request that directly gets mapped to a view, so no controller is involved
Type B - this is request that maps to a controller
For both type A and B a view always reads data from the models directly
If it is a type B request, the controller reads/updates models and when done asks the MVC framework to return a view to the client. The view reads the update models and renders to the client.
This is the approach supported by the Induction MVC framework.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With