Explain MVC to me assuming that i am supremely thick!

Soldato
Joined
3 Apr 2003
Posts
2,928
Hey guys,

I have been programming for a while and despite this question I am actually fairly proficient in c++/c#/java. By fairly i mean i have finished my second year of uni on computer science and made some programs but if i am honest i still struggle to get my head around some concepts. I can code, but dont fully understand the code if that even makes sense.

Now i have decided to teach myself asp.net over the summer as i figure i would like to move towards web dev as a career, however when i read about things like MVC i just cant get my head around exactly what it means no matter how many explanations i have read. So apparently its separating the visual aspects, logic and data? But i still dont really understand the concept. How do you know if you are following MVC or not?

I dont even know if this question makes sense lol, its so hard for me to explain what i mean i just dont quite grasp these conventions.

So please can somebody explain MVC to me with some examples maybe that will help me to understand it better? Thankyou.
 
Soldato
OP
Joined
3 Apr 2003
Posts
2,928
VMC ideas as I understand and from experience of using it in real world situation:

Models contain your data, for example if you have a car dealership website database, your model would be cars where you might have functions such as getCarByReg(), getAllCars(), getCarByPrice()

Controller would be something that will call those model functions, so when a user wants to view a list of cars, you would handle that at the controller level. You would have your "action" functions there for example listAction, viewAction, searchAction

And view would simply contain your html templates into which you could insert what controller retrieves from the model.

Its a very rough overview and if you are using it for a simple application, there is 0 use in this really. You would spend longer setting up your framework to make use of it for little gain. Its main use is in larger projects. For example content management systems, where your models might be

cms_items, cms_users, cms_permissions, ect.

Cheers matey appreciate the effort.

I did google this beforehand but the explanations are all kind of abstract and its that part of it that i really dont understand. Does it mean seperate the functions into different files is it all just a conceptual thing and no bearing on the files i am creating etc.

As for worrying about my course, i worry about it myself. More worrying is the fact i am one of the stronger programmers in the year. The code comment i made in error, i understand my code, its the concepts i am having trouble with. I get OOP etc but when comes to things like MVC i am just not sure what it really is. Figured better off asking here whilst i try to get better over summer.

There is no academic help to be had as i am having no trouble doing my assignments, this is something i wish to work on in my own time.
 
Last edited:
Soldato
OP
Joined
3 Apr 2003
Posts
2,928
I've been programing professionally now for four years and have to say that I learned more in my first 6 months of industry than I did in 4 years of uni. So with regards to your proficiency claims I would raise an eyebrow. None the less I've found Jeff Atwood's coding horror blog very good at explaining various parts of the .Net framework and how MVC should be used http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html another good resource is Scott Hanslemans blog, I'm not sure if he has any MVC posts but in general its very good reading for all things .Net

Proficiency was definitely the wrong word. I understand the basic syntax and can create basic applications using these languages, hence my assignments and such. But i simply dont feel like i know enough to do anything meaningful.

As everyone else on the course seems to be in the same boat or worse, i am assuming that its just something that i will not fully understand until i make a start on proper projects in the industry, but i hate feeling like i dont have the knowledge or ability to do something properly so am just trying to improve on the fundamentals.

Quick edit to thank you for the link, that was very helpful and i understand it a 'little' better now. I understand seperating the logic from the visual to keep it skinnable etc. The controller is still a bit of a smudge for me, as at one point 'the browser is the controller' but in another they are on about more logic to manipulate the data which i thought was down to the model lol. Either way the 'skinnable' test is a good starting place for me so thanks very much.
 
Last edited:
Soldato
OP
Joined
3 Apr 2003
Posts
2,928
The controller is the first thing that gets hit when you go to a certain URL. Say I owned the site foobar.com. I might have users on my site and users may want to view their profile. The URL for that could be foobar.com/user/profile. That would hit the "user" controller and call its "profile" action. An action is just a method (a function belonging to an object) that will dictate how to handle a request. It will delegate the actual legwork to models.

At a high level models are objects that hold some user input and methods to work on that input. I might have a contact form. This is going to take data from the user and that data needs to be sanitised. For example I might like to verify they've provided a valid email address. I could setup some validation rules in my contact form model. So a controller's action will take input from the user, pass it to the model then ask the model if the data validates. If it does then the action tells the model to send myself an email containing the user's message. Assuming everything went well I'd probably like to thank the user for taking their time to contact me.

Views are just the HTML for an MVC application. They can be compromised entirely of HTML but more often contain a bit of server-side code for stuff like iteration (e.g. looping over a bunch of forum posts) and simple conditional statements. The data they work with will be models.

Closely related to models is the Active Record design pattern. This is the idea of an object in code representing a row in some database table. Going back to the user's profile, the controller would pull the user's details out of the database by loading a model by its PK then pass the model to the view to display profile information.

Hope that helps. Explanations only go so far though, you need to get your hands dirty :)

Wow thanks very much thats a massive help :)

Will just keep practicing and practicing until i get it all down!
 
Back
Top Bottom