MVC Explained

Since the last post, a few people have asked me if I could write some posts which will help explain some basic concepts to beginners, as well as posts that are a bit more technical.

I am just starting to learn Rails which is a framework for Ruby so as I go through I thought I would write a few posts which break down some of the fundamental concepts, partly to make the blog easier to follow for people who are just getting interested and partly because it will help me understand and re-enforce my learnings...

To begin with I should give a very brief explanation of what Ruby and Rails well... actually are (referred to collectively as 'Ruby on Rails').

Ruby

Ruby is a programming language, so basically a way of writing code which follows particular rules and constructs specific to Ruby. If written so that it follows the rules of Ruby you can make your computer do what you tell it to.

Ruby is an open source (open source means free, free in the cash sense but also free in the freedom sense i.e freedom to distribute, share and contribute to) object oriented language where everything is an object.

Objects are basically chunks of code functionalities that you can instil with identities, properties, and attributes. When you work the right objects together (often by making use of the right data structures and conditionals) you build up your program to do what you want it to do. The fact that everything is an object means that you are armed with the power of greater flexibility when designing your program (figuring out how you will get it to do what you want).

If you are thinking of starting to learn a programming language I would definitely recommend Ruby as of course it very powerful, flexible and enables you to make lots of cool stuff; but visually, compared to many other languages it is a bit easier to read. It uses a lot more 'english' words which, when you are learning the different constructs and functionalities just makes life a bit easier. That being said it is still complex and there is tonnes to learn so don't feel disheartened when the shouting at the computer screen/crying into the keyboard happens... character building!

To try out Ruby for yourself and learn more check out: http://iwanttolearnruby.com/

Personally, I have been reading a book called 'The Well Grounded Rubyist' by David A. Black which takes you through all the main foundations in a way which is really clear and thorough, with loads of interactive examples so definitely worth a read.

Rails

Rails is a Ruby framework that is used when developing for the web. It provides you with a set of web-app-making functionalities which is great because you don't need to keep coding them out each time you make a web application. This is given to you in the form of a bunch of libraries which work harmoniously to pull your application together and deliver onto the world wide web.

Rails is implemented using Model View Controller (MVC) architecture. This means that the Rails model is split into three components

Model – maintains the application's state

View – generates the user interface

Controller – receives events from the outside world (usually user input)

So let's walk this one through:

  • You are at your computer looking at a website and you see a nice link to an article about a new water park opening in your home town complete with a countdown timer.
  • You are intrigued so you click the link to the article.
  • This sends a request to the controller (as we know this is the place where events from the outside world go).
  • The controller interacts with the model (which we now know maintains the application's state). The model acts almost as a gatekeeper enforcing rules that apply to the data received from the controller and determines what to do with it. Maybe the model could enforce a rule that ensures the countdown timer only displays if the user visits the page before the grand water park opening.
  • The controller then invokes a view (which we know generates the user interface), this displays to us the water park article and countdown timer in the browser.

So in a Rails app an incoming request (HTTP GET or POST request) is sent to a router which figures out where in the app the request should be sent and how it should be parsed.

This will identify a particular method (also called an action) written in the application's Controller code.

That method may look at data submitted by the user, interact with the model to run some business logic checks or it may invoke other methods. The correct view will then be invoked via the judgement of the controller and will render in the browser for the user to enjoy.

If you want to learn more about learning Rails then there are plenty of resources online... just get googling. Though I have heard good things about: http://railscasts.com/

I am currently reading through a book called Agile Web Development with Rails 4 by Sam Ruby, Dave Thomas and David Heinemeier Hansson which I would highly recommend ;)

So why use an MVC?

MVCs are used primarily for making things quicker and easier, saving yourself hours of potential pain trying to manually write stuff that has already been written, to get your application to render appropriately.

Building an application using the MVC format results in code which is well structured and easier to maintain.

Also in a world where we consume media on a range of devices, the controller could suss out from which platform an application is being browsed on and generate a view optimised for that particular platform.

MVCs are useful for large scale team based development. If your client is asking you to complete a load of changes and you didn't use an MVC you would have a lot more work to do as you would probably have to modify a load of classes and try and keep track of it all. With an MVC you can switch things in and out much more cleanly.

The disadvantages could be that developing using an MVC can be difficult as it takes some time to get your head around using and working with. There is also danger of things being too separate and not communicated properly between UI people, business logic people and whoever writes the controllers.

Rails is not the only framework to make use of MVC architecture. Other examples include:

Python – Django

PHP – Cake PHP

PHP – Zend framework

PHP - Symfony

ASP.NET MVC

If you would like to see some examples of sites built using Ruby on Rails then I found this website with some examples: https://netguru.co/blog/posts/top-10-sites-built-with-ruby-on-rails

So overall I hope that has helped to shed some light onto what Ruby, Rails and MVC are and has provided a vague outline on how they can be used to form the software architecture of web applications. As I learn more and share my building of some wondrous rails apps you will have a better idea as to what I am going on about. Please feel free to ask me any questions or add to this!