Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Hat Is MVC

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

hat Is MVC?

Author: Jeremy

Morgan

APR 10TH, 2013

The MVC or Model View Controller architecture is a software pattern


thats become very popular over the last few years. With this article well
take a look at MVC, how it works and how its used.

What is MVC?
MVC is not really a package or a tangible object but more of an idea or
methodology. Microsoft has a software package for ASP.Net
called Microsoft MVC but its not really a product as much as an
implementation of the pattern. There are other projects out there similar
to this, but you can build a Model, View and Controller in any language.
Its simply a way of building software that has many benefits, heres a
rough idea of how it works:

The MVC Model consists of three parts:

Model - Usually tied to a database or other data source, this section


only deals with raw data in the application.
View - This is the display functionality of the application. Usually a web
page or User Interface.
Controller - This is what ties the other two together. It takes commands
from the user, retrieves or updates data in the model and communicates
information back through the view.
When these three parts work together you get a separation of concerns
in your code. You have a model which only cares about itself, it merely
processes requests and manipulates or sends data. The View is
concerned only with displaying the data and providing a way for the user
to communicate their next move. The controller merely coordinates the
two.

MVC on the Web


So how is this applied in real life? Heres a diagram of a web site using
MVC:

First the visitor opens a web browser, and a command is sent to the
controller to show a web page. The controller then contacts the model,
which pulls raw data from the database and passes it back to the
controller. The controller then passes it to the view which formats the
data and mixes it with html to create a web page you can view.

Advantages of MVC
MVC frameworks do not offer any additional performance or features to
your application, they exist primarily to help the programmer(s).
Separation of Concerns: - This is a tenet of good design and
encapsulates your functions and creates a defined interface. For
example someone working on a model only has to worry about
interfacing with a database and getting the right information, but
doesnt have to worry about browser compatibility at the same time.
Code Reuse: - Because of the separation of concerns with MVC you can
reuse your code easily. If you have a great view object that displays
pages well on a phone for example, you can reuse that same code easily
as a view in another application.
Decoupling: - Another tenet of good design, keeping your software
decoupled means easier changes down the road. For instance if youre
changing database servers from one vendor to another do you want to
rewrite or modify your model, or the whole piece of software? With a well
implemented MVC framework you can do it in a fraction of the time.
Testability - While having your software decoupled and separated is
good for programming, its great for testing. You can take a section of
code and easily run various tests on specific parts of the program as you
go along and those tests can be very focused. Are you looking for data
integrity? Expected display? When functions are clearly grouped testing
becomes far simpler and more organized.

Disadvantages of MVC
So if MVC is the greatest thing since sliced bread then why isnt
everyone doing it all the time? Its a mistake to think MVC is a magic
bullet that will cure all your software problems.

Complexity - MVC adds a layer of complexity to any project. This is a


cost thats acceptable with a huge payoff and that usually comes from
large projects that would be complex anyway. But a small app or widget
is better off without such complexity. Sometimes MVC just adds more
work to a task that would otherwise be simple.
Can affect performance - If you have a simple widget that uses 20
lines of code and a simple task it could very well be slower when done in
an MVC environment. Again it comes down to cost vs benefit, and MVC
doesnt always win.
You may be using non MVC code in your project - You could be
stuck with code that is already built with model and view functionality
tightly integrated. Trying to mix and match in an MVC model can cause
more problems than it solves.
Additional Resource Usage - MVC frameworks are avoided in low
resource environments such as embedded software and programs
designed to run very lean. When youre building a web app on a big
server the fractional increase in memory usage is well worth the benefits
but if youre counting bytes or squeezing something onto a chip its best
to avoid them.

Summary
I hope Ive given you a general idea of what MVC is and you may even
be considering it for your next project. Generally the following projects
are best suited for MVC frameworks or design:

Large web applications


Large projects
Projects with multiple programmers
Projects with separate designers and programmers
Anything that may scale larger in the future
Projects that will have an API

In future articles I will go over how MVC is changing and being


implemented in todays application. Its been around since the 80s and
the classic MVC model is changing and adapting to new trends. Ill also
do a tutorial showing how to do a basic MVC design with PHP. Till then
send me your comments and questions if you have them!

You might also like