Archive for the ‘concepts’ Category

MVC in simple terms…

June 20th, 2011 No comments

Recently a friend of mine asked me what MVC was.  I know it took me several years to understand mvc…  So, I sent him this email which I feel pretty well explains it…

Warning…  What follows is completly my un-educated experience…  So, other’s may disagree, but I’m pretty sure this is atleast 80% accurate…

As far as classes, files, etc…  That really isn’t important.  That is more of an implementation detail.  The real key, is the concept of separating the model, the view, and the controller, whether that is done in a scripting language that doesn’t support classes in seperate files, or seperate classes in one file, whatever, while it is important to seperate them somehow, the HOW isn’t as important as the WHY, and WHAT.

In abstract terms…  The model is really where the application is.  The controller is the glue between the request, the model, and the next view, and the view is just the output of the model.

i.e.  in a perfect MVC application, you could write a command line version of the application by writing a new “controller” that takes it’s input from the command line, and new “views” that return output to the command line.  And all the business logic would live in the model.  The final version of the app was DEFINITLY NOT perfect MVC, however, you can see how at the top of the file we do all the “controller” stuff.  The Model is really mixed in with the controller stuff, although the detailed db stuff is abstracted out by Infusionsoft.  The view however is pretty pure.  Not much logic in the view.

Now, this is NOT to say there should never be logic in the view, but there shouldn’t be “business logic”.  You can have logic in the view to display select boxes, or to check a flag to decide how to display something, but the view should do what it is instructed and be pretty dumb, and really not have a clue about the internals of the model…

So, another way to look at MVC is a car…  The steeringwheel, brakes, and turn signals are the controller (the human interface INTO the car, although this is a bad example, because in a webapp, the physical manifestation of the steering wheel is actually in the view, but the hidden parts of the steering wheel that connect it to the mechanism that turns the weels is the controller, and the thing between the connection with the steering wheel and the wheels is the model).  The speedometer and dash are the view, and the engine / driveline is the model…  The speedometer doesn’t know ANYTHING about the car, all it does is display the signal that comes into it.

Another example is an elevator…  The control panel is the link between the buttons and the elevator computer, the model is all the elevator mechanisms…  And the view is both the light up buttons on the control panel, and the floor indicators most elevators have.

It’s a little difficult to use real world examples because the physical manifestation of the controller is really part of the view, whereas with webapps, the controller is the thing that takes the request and controls the model.  And the view is used to send the right information to the controller, as well as return output to the user.


Categories: concepts Tags: