What is MVC ???

So lets just dive right in. No **** talks. Just the things needed. :P

First and foremost I have to tell you this. Most of the things I explain here (at least Im trying to :p)
I learnt by my self. No other parties. Just me and the sweet old internet spending long hours with my poor baby laptop.

Unlike other stuff, When you try to understand something like MVC without any help from others, I`ve gotta tell you thats something tedious and that will probably make you wanna quit your life, literally. So one time I stopped learning this MVC thing. But you never give up anything in life and become quitter, DO ya ???. Now after like 2 years I have build CodeIgniter, OOP MVC framework apps and ASP.NET MVC 5 apps.

Bottom line is dont quit, you will get it eventually. :D


What is MVC?? 

"MVC is Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user." 
Thats Wikipedia right there :p
Ok.Lets make it simple as possible.

Imagine you are developing a large scale application.A web application. You cant do it alone because its gigantic. Lot of developers have to work together. And lot of languages have to be integrated. Interfaces have to be written in HTML,CSS,JavaScript. For databses you will have to use SQL. For application you may have to use, C# or Java or PHP or anyother programming language that is available.

So if you don't use these things carefully separated you will have to use all these languages and developers working in one place. You will make a quite tangled ugly mess out of it. :D
Imagine a source code where HTML,CSS,JS,PHP,SQL is there. Thats just 5 languages right there. Thats a developers nightmare.If you want to have a clean neat development process you need to separate these various elements.

Thats what MVC has done. Thats it. No big deal. 

Ok lets dive hard again.

M is for Model. All the Database related things will be done here. (SQL and programming language.(eg: JAVA))
V is for Views. All the user interface related things will be done here. (HTML,CSS,JS)
C is for Controller. All the application logic is written here. (Java or PHP or C# or Python or whatever prgramming language)

When we seperate things like this we have the following advantages.

1. Database logic and UI logic and application logic is not mixe up. Simply programming languages dont mix.

2. Database people can work on Models, UI guys can work on Views and Other developing guys can work on Controllers at the same time.
 Because it is separated.


But although  M and V and C is separated from each other, in order to application to run these things have to be linked together somehow.

This is how it is done.
For example lets say we want log on to facebook and this is what happens according to MVC.(This is just the simple version)

1.You enter www.facebook.com on your browser. (according to MVC you are requesting a view.  In other words a web page)
2.Your request will go to the facebook controller.
3.Controller will send you the requested login web page.
4.Now you have the Login page (a View). You can interact with the views.
5.So you enter you email and password in the view (web page) and sends it to application. (In this case your application is facebook)
6.This respond is sent to a Controller again.
7.And controller now knows this web page needs to have some sort of database things done.
8.So the controller connects to a Model.
9. Now the Models does all the database operation. (looks for a user in the DB with given email and password)
10.If there is a user with that login, Model sends these data to the controller.

11. Now controller sends the related web page to user with those details that was recived from the model.
12. VOLA.! now you have your facebook profile page.


Take notes on these.
# Users dont intearct with Controllers and Models. They can only interact with the views.
# Contoller can be the broker between Model and Views.
# Model is not a necesity for an application to work. You can make it work with a Controller and Views. But there is no point in having an application 
without a Database.Is there ???

Thats MVC ladies and gentleman. :D
Share:

0 comments: