Randy Gurment
Randy Gurment

Reputation: 177

Correct implementation of MVC architecture

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

Diagram 1

Diagram 1

Diagram 2

Diagram 2

Diagram 3

Diagram 3

Upvotes: 5

Views: 2342

Answers (3)

bluecarbon
bluecarbon

Reputation: 326

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.

Upvotes: 1

mkorpela
mkorpela

Reputation: 4395

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.

Upvotes: 8

Charles
Charles

Reputation: 3774

My strategy for learning good MVC techniques was to find someone who knew and ask a lot of questions. Asking a bunch of us who do not know your requirements, your intentions, or your ideas does not do a lot of good.

Its my opinion that Diagram 1 would be considered the 'best' diagram, but without knowing your unique situation it would be better to explain your needs to someone else who knows your requirements and MVC architecture.

Upvotes: 0

Related Questions