This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

So What is MVC about?



 

Well lets start with what MVC is and means in its most basic form it’s an idea, and idea to keep code clean easy to follow and reusable.
 
Well lets cover MVC the term and what it mean MVC stands for Model Controller View now these set a few simple rules in your programming that are known as the design pattern if you look at the image i have used for this group on P2PU you will see the Design Pattern in a simple diagram 
 
Let’s go into a bit of depth with MVC
So a Model is known as the business logic this is where the majority of your applications processing will occur so if your connecting to a database you would implement a model to do the handling of data that in the database your create, updates, drop’s and select ect...
 
So now the View this is quite well as the name states is a view a method of displaying the information to the user nowadays applications are using fewer and fewer views as AJAX and JSON/XML take over the communication from browser to server but never the less you always need one at least one view 
 
and last of the mark is the Controller this is where you application with bind your Models and Views together so your controller for example would call your database model that selects 20 rows they are returned to the controller you would then pass them into your view that would handle displaying that information
 
So why use Lightweight MVC for the example 
one of the things i worked on was making that transition between Controller and view as easy as possible Lightweight MVC will expose any publicly declare variable to be accessed by the view so in lightweight i have tied the controller and view to a very close state some my think its to close but the point was to let users under stand the basics of MVC and not dilute there learning with learning an class / object on handling there view
 
So what are the benefits to using and MVC
Well besides the basics show above about easy and tidy code they support massive amount of support for code reuse now this does not mean a simple copy and paste and there we have reused the code it means that where we have a Database change method of a model we could call that very change on 20 or more pages where as a static coded file we would have to copy and paste the code to reuse we would simply call upon our method normally 1 line that points 20 or 30 lines so instead of using 20 or 30 lines 5 times we use them one and then use 1 line 5 times
 
 
Are there times MVC Architecture should not be used
yes, i have seen code where MVC was implements wrongly and for the reason of the code was built in MVC, please remember object are resource heavy if your creating an object that holds one method that only called once it is a wast of your servers vital memory and as such could prevent users from using your application due to the fact it runs slow or there not always able to connect i always recommend a plan of action think about the project your embarking on will it be reusing code, will it be high in memory consumption although not as tidy code a static scripted file uses a lot less memory for one thing its one file your not opening 5 or 6 files reading them in and using very little from them.
 
A Small task
At the end of this i would like you to come up with a mind map outline how you would build company staff management application all this application need to do is allow a manager to keep track of full day absences, the staff he has working for him and is outgoing for his staff all on a fixed salary, I don't mean i would like to see you build a system that does that, just a mind map with 3 starting points Model, Controller and View and show a little description about what models controllers and views your would use and how they would link together 

Task Discussion