ad

Monday, 15 February 2016

MVC Model View Controller | Seriously Simplified Version

Model View Controller Seriously Simplified

I've written about MVC in past..
http://www.webdevtown.com/2015/04/model-view-and-controller-with-example.html
So, recently I was going through that past tutorial and decided to re-read it, to my surprise it came across as not very easy to understand, so here's another quick attempt. Hope this one helps it understand.

Okay. You're a web developer who develops everything by himself, from design to complex coding, it's all done by you. No big deal, you get the idea of what you want to develop and start writing the code, function by function, time comes when you've got 100s of files thousands of functions and it's a freaking mess, isn't it? where do I change code to update row in table? I want to make sure data is cleaned. Wait, what's this HTML doing here? hmm I need to make it easier to get usernames and other data from DB.. okay I might update the code, but it'll require my to update the logic too.. hmm it's a freaking mess.. wait hmm.. I'll be selling this product to a fellow developer.. I wonder how he's gonna deal with all this tight coupling.. I've glued everything together.. Hope he doesn't hire designer to update the template.. it'll be nightmare, he might come back to my saying code's a crap.

That's why you need MVC. Writing code alone or with someone, it it isn't clean it's just.. pain..

There's three things you need to know and know it well enough. Here's the start:

Model

Model's a class that represents all your data. It has methods that update, delete, retrieve data from DB. You could have class that'd retrieve recently signed-up users from DB, or any other data. You want data, create object and get it. You want to delete user, pass user name and it deletes user. Everything nice and tidy. It's mostly about the DATA!! Not manipulation or other stuff but about Data!

View

View is what you see on webpage, HTML, forms, titles, tables, buttons. It all belongs to View page. It has minimal script. It's mostly HTML. Don't add bunch of code to View unless you want to piss yourself or designers in future and hear them telling others your code sucks!

Controller:

It's piece of code that sits between View and Model. User hits Submit button from View, Data goes through Controller, you know, you might want to do some validation, manipulate, twist it and whatnot. It all happens here, once controller's job's done. It can send data to model if it requires database manipulation or information retrieval, or it can return data back to View, where View will present it to user. If app required data from Model or needed to make some changes to data, it will be sent to Model and then Model class would send it back to Controller, then Controller to View for Presentation. View wont interact with Model directly, it has no business to do so..

Whole purpose of this MVC pattern is to simplify(Ahm! OOP) things up and make it easy to manage the code in future. Before you start with MVC it's necessary you understand OOP and have created an app or two using it's fundamentals. MVC isn't really a need per say, but someone who's just writing couple hundred lines of basic code it's good to know that MVC exists so when you feel your code's getting messy, you know what to do about it!

No comments:

Post a Comment