CSC/ECE 517 Fall 2013/ch1 1w26 as

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction to MVC

MVC architecture is a design pattern which separates the representation of data from users interacting with it.<ref>http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller</ref>

It consists of 3 major components:

  • The model: In MVC, M stands for Model. A Model is a collection of Objects which encapsulates the data in the database and provides an interface to access it.
  • The view: A View is called the presentation layer, and this is where the data from the model is represented generally on a web browser.
  • The controller: A Controller handles the decision making process and communicates with the Model to access the data.

This kind of architecture is very popular among web applications.

MVC architecture simplifies and modularizes the approach to creating a web application. Now let us discuss how MVC design pattern is supported in Rails, and how does it differ when compared with other popular languages.

Fig 1:Figure showing how Model, View and Controller works in the background

MVC in Rails<ref>http://www.cs.toronto.edu/~wl/csc309/handouts/rails.pdf</ref>

To understand how the MVC architecture works in the background, let us take an example of web browsing. The following is the sequence in which the process goes about:

  1. The user enters the URL of the page he wishes to visit.
  2. The controller interacts with the model to fetch the particular request and invokes the view to render the new view.
  3. The view renders the required web page and displays it to the user/client.

In Rails, the model, view and controller and separate entities functionally which are knitted together to perform the required functions.


Example from a Rails application:

  1. Incoming requests are sent to a ‘Router’ which determines where to parse the request and forward it.
  2. The request if forwarded to a controller, wherein a particular method/action is called.
  3. Using the data available in the request, the action interacts with the model or it causes other action to be invoked.
  4. The action then prepares the information for the ‘View’ which renders the output to the user.

Rails handles all the low-level details which would otherwise take so long to do it by self, so that you can concentrate on the application’s core functionality.

Rails principles

  1. Convention over configuration: By using standard conventions, we can avoid the need for explicit mapping such as deployment descriptors. The naming conventions would automatically perform mapping (like between controller and view)
  2. Don’t Repeat Yourself (DRY): While using Rails, we make sure that the required information we might user time and again to execute the same functionality is stored in a single unambiguous place.

Inside Rails

  1. Active Record (Model) - This is the model support in Rails. In the ORM<ref>http://en.wikipedia.org/wiki/Object-relational_mapping</ref> layer supplied with Rails - tables map to classes, rows to objects, and columns to object attributes.
  2. Action Pack (View and Controller) – This bundles both the view and controller components which are quite intimate. The controller supplies data to the view and also receives events from the pages generated by the views.

View:

Action Pack provides View support by helping create either all or part of a page to be displayed in the browser. The dynamic content is generated by templates.

Controller:

Coordinates the action between the user, the views and the model. Rails handles most of this functionality behind the scene and lets you concentrate on the application-level functionality. The controller also routes external requests to internal actions, manages caching, manages helper modules and sessions.

MVC applications in popular programming languages

Ruby

In the recent times Ruby on Rails has become a popular<ref>http://en.wikipedia.org/wiki/Programming_languages_used_in_most_popular_websites</ref> choice for creating applications on the web. Its popularity is mainly attributed to its ease of use, and also the short time it takes in creating and deploying an application.

Following are the list of famous websites<ref>http://www.developerdrive.com/2011/09/20-best-sites-built-with-ruby-on-rails/</ref> that have been built using the Ruby on Rails framework

Java

In the Java world, the MVC architecture for web applications is achieved by using the Spring MVC<ref>http://zeroturnaround.com/rebellabs/the-curious-coders-guide-to-java-web-frameworks-spring-mvc/#!/</ref><ref>http://www.quora.com/Java-programming-language/What-are-some-famous-websites-written-in-Java</ref> framework, along with an ORM tool like Hibernate.

Following are the list of popular websites<ref>http://thewhitecover.com/10-best-high-traffic-sites-built-in-java-servlet-including-gmail-and-amazon/</ref> which have been implemented using the Java Spring MVC framework

JavaScript

In the JavaScript world, MVC architecture is often achieved by using a 3rd party library. Some of the popular javascript libraries<ref>http://codebrief.com/2012/01/the-top-10-javascript-mvc-frameworks-reviewed/</ref> in use to create MVC applications using JavaScript are Backbone.js, Ember.js, Cappuccino, Spine.js, JavaScript MVC and Google Closure.

PHP

As with JavaScript even PHP has a ton of 3rd party frameworks<ref>http://jonathanmh.com/best-php-mvc-frameworks-of-2013/</ref> offering the much need MVC architecture support. Prade, Seagull, CakePHP, WASP are some of the popular ones.

Following are the list of popular websites<ref>http://susprod.com/famous-sites-that-use-php/#.UjnaLsZ6Zec</ref><ref>http://stackoverflow.com/questions/1906795/what-are-some-famous-websites-built-in-django</ref> which have been implemented using the MVC framework through PHP:

PHP is the most commonly used web framework on the internet. This trend has observed some variation in the last decade due to the gain in popularity of Object-Oriented languages such as Java, Ruby, Python, etc.

Python

Django is the goto framework for implementing web applications following the MVC architecture in Python.

Following are the list of popular websites<ref>http://lineofthought.com/tools/python</ref> which have been implemented using Django and Python:

Differences between the MVC architecture as compared to Ruby on Rails’s and other popular frameworks

Most MVC frameworks have a lot of features in common<ref>http://stackoverflow.com/questions/9179848/do-mvc-frameworks-vary-greatly-from-one-language-to-another-ie-asp-net-python</ref>, and talking about which framework is the most suited one to build a web application is a highly subjective topic. In most cases the answer to this question is derived by thoroughly analyzing the case at hand. For instance enterprises whose applications are already running on the JVM platform would often go ahead with the Spring MVC framework coupled with an ORM tool like hibernate. New startups which are set up in a fast paced development environment often prefer to work with Ruby on Rails as it follows the dry (don’t repeat yourself) approach. Now let us see where exactly the differences lie amongst these frameworks

Convention over Configuration

Ruby on Rails follows the rule ‘Convention over Configuration’. So this typically means that there is not a lot of configuration that has to be done. The framework has a number of naming conventions pre-defined and using these in our application as per the rules would get our application up and running in no time. In a framework like Spring MVC we generally have to do with a lot of XML configuration, and we would have to write a lot more code than we do when using the Rails framework.

Presentation Layer(View)

With the Ruby framework the Views are generally a mix of HTML and your regular ruby code. But take the case of the Django MVC framework used with Python. In Django we use a template language to define the View component.

Level of Modularity

Many MVC frameworks offers many type of View engines to work with. For example Spring MVC. Whereas other frameworks like Ruby offers only one way to do it. This actually decreases the level of modularity for an application.

Full MVC vs Simple MVC

MVC frameworks can typically be classified into two types. Full MVC and Simple MVC. Full MVC frameworks go beyond the concept of model-view-controller, and offer additional features to build more complex web application. If you consider Rails, Spring MVC or the Django framework. All these frameworks offer a standard ORM (Object Relational Mapping for data access). Spring in addition also offers an IOC (Injection of Control) container while Django offers an interface for the model. If you consider these frameworks to be Full MVC’s then ASP .Net’s framework leaves out the modules like IOC and ORM, and hence can be referred to as a Simple MVC.

Language dependence

Most MVC frameworks induce the restraint of using a specific programming language. For instance Rails requires one to program in Ruby, whereas using Django required the developer to program in Python. However using Spring gives the option of potentially using any programming language that can run on the JVM (java virtual machine).

Conclusion

So based on the above differences if we have to make some conclusions then we what we have at hand is that while using a framework like Rails we can get up and keep running quickly, but could run into problem if and when we try to venture outside the framework designed boundaries. Spring offers a complete enterprise platform which in a few cases might be a overwhelming set of features as many of them are largely unused. Choosing a simple ASP .Net framework would set us up with a nice web layer, but apart from that rest of the application plumbing work would be a responsibility of the developer.

References

<references/>