Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct implementation of MVC architecture

I read a lots of articles about MVC architecture, but I'm still confused.

  • Which diagram is correct?
  • Does even exist correct implementation of MVC architecture?
  • Could I use diferent implementation of MVC architecture?

Diagram 1

Diagram 1

Diagram 2

Diagram 2

Diagram 3

Diagram 3

like image 832
Randy Gurment Avatar asked Apr 18 '10 19:04

Randy Gurment


2 Answers

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.

like image 87
mkorpela Avatar answered Oct 01 '22 01:10

mkorpela


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:

  1. 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

  2. For both type A and B a view always reads data from the models directly

  3. 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.

like image 29
bluecarbon Avatar answered Oct 01 '22 03:10

bluecarbon