CSC/ECE 517 Fall 2009/wiki1b 13 za: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 21: Line 21:


=== Original MVC ===
=== Original MVC ===
[[http://stephenwalther.com/blog/images/stephenwalther_com/blog/WindowsLiveWriter/TheEvolutionofMVC_CBBD/image_2.png]]
Include this image [[http://stephenwalther.com/blog/images/stephenwalther_com/blog/WindowsLiveWriter/TheEvolutionofMVC_CBBD/image_2.png]]


The original MVC pattern is very simple and have a very different path for the data, request and response of the user. Here the Controller modify the Model accordingly but does not interact with Views. The Views are updated directly by the Model. When the Model changes during an event, an observer design pattern is implemented to change the View in response to the event. Thus the View is continuously observing the Model, and gets updated.  
The original MVC pattern is very simple and have a very different path for the data, request and response of the user. Here the Controller modify the Model accordingly but does not interact with Views. The Views are updated directly by the Model. When the Model changes during an event, an observer design pattern is implemented to change the View in response to the event. Thus the View is continuously observing the Model, and gets updated.  

Revision as of 17:19, 20 September 2009

History and Applications of the MVC Pattern

MVC - the buzz term!

Image taken from www.symfony-project.org

MVC (Model-View-Controller) offers a framework consisting of a Model that enforces the logical rules that should be applied to the data. A View is pertaining to the user-interface. It helps the user to interact with the Model. Finally the Controller acts as the point of communication between the Model and the View, taking inputs or requests from the user and manipulating or processing the underlying data accordingly to provide the user with the desired outputs. For example, in an e-commerce business setting, a model will make sure that the shipping charges are correctly applied, a view will generate pages displaying the items available and a controller will make sure to add selected products to the cart of the customer.

MVC offers a clear separation of the presentation logic from the business logic. This is the main reason, why it has been widely used architecture for web-based application. With an MVC framework, it becomes easy to accommodate future changes and implementations in design or code. MVC pattern gained prominence because of its clean modular approach that allow programmers to follow a formal approach for implementing business or enterprise level applications. Though, it is worth mentioning that using the MVC methodology for designing applications requires a lot of planning in terms of working out the separations in design and how the modules would interact. What is achieved by keeping these functionalities separate is reusability which is one of the major advantages of MVC.

Evolution of MVC

MVC Architecture

Over all these years, history of MVC architecture has become dim. MVC was invented at Xerox Parc in the 70's. Trygve Reenskaug was the one who wrote original notes on MVC in 1978 but he never got to publish them. His first note on the MVC pattern consisted of : Model, View, Controller and Editor. The 'Editor' is a short-lived component that the 'View' creates when necessary. This component is created as an interface between the 'View' and the input devices. After Reenskaug left Xerox PARC, Jim Althoff with a group of other people implemented a version of MVC specific to the Smalltalk-80 class library [Ref [1]].Even though they changed his implementation and removed the 'Editor' , Trygve Reenskaug was credited for his work.

MVP Architecture

After the implementation of smalltalks-80 MVC, there were a few downsides to MVC approach and so a new variant of MVC was introduced MVP (Model View Presenter). It was very similar too MVC controller. The only difference lies in the way presenter is linked to the associated views. But over time, the difference got more obscure and now implementations of MVC frameworks follow more concept of MVP[Ref [2]].

MVC Design Pattern

Ward Cunningham and Ken Beck who were working with Smalltalk were the first to start the MVC design pattern in 1987. The MVC design pattern is different from the MVC architectural pattern. In fact there are many other design patterns - Analysis, Creational, Structural, Behavioral [Ref [3]]. More information on the use of MVC as an architectural pattern or a design pattern can be found at [4]

Comparison of the MVC implementations in various web-based frameworks

MVC being a framework can be exploited to work with various languages. A list of such languages would include C++, JAVA, Ruby, Flex, DOT NET and many more [Ref [5]]. We make an attempt here to compare the different implementations of MVC, with respect to the different programming and markup languages. But all the implementations of MVC fall into three categories, Oringinal MVC framework, Model 2 MVC framework and Web application framework. So we will look at each implementation and compare them.

Original MVC

Include this image [[6]]

The original MVC pattern is very simple and have a very different path for the data, request and response of the user. Here the Controller modify the Model accordingly but does not interact with Views. The Views are updated directly by the Model. When the Model changes during an event, an observer design pattern is implemented to change the View in response to the event. Thus the View is continuously observing the Model, and gets updated.

This original pattern helps in Separated Presentation. According to Martin Fowler, Separated Presentation means "Ensure that any code that manipulates presentation only manipulates presentation, pushing all domain and data source logic into clearly separated areas of the program". [7]

Original MVC Model is very simple to understand and implement too. It was the early approach for web-application development but still used for simple application. It is used for quick development. But the relation between View, Model and Controller, makes it difficult for different teams to work on it. Thus even though implementation is easy, work can not be divided.

Not many web-based frameworks use this approach. JavaServer Pages (JSP) uses this approach. This approach is also called page-centric approach also. Here we only build JSP pages which interact with users and handle In the Model 1 approach to creating a JSP application, browser requests are handled by a JavaServer Page directly. These pages only send the request further for processing. These pages go ahead and display the dynamic content generated [8].

Model 2 MVC Framework

Include this image [9]

This framework is used by mostly all the web-based implementations. MVC framework is now considered as this model only. In this model, there is no more contact between Model and View. Everything happens with Controller at its center. Controller is responsible for co-ordinating the application.

Whenever a request arrives it goes to Controller first, then it passes the data to Model for validation and handling of data. Then if Model needs to pass some data back to the View, it also passes through Controller which invokes View.

Model 2 MVC framework helps you to create complex application easily. This approach is used for web-based application which need to be modified regularly and maintained for a long time. It helps in development also, as View and Controller are so distinct they can be worked by different teams. And with simple View, the designing team can do wonders without affecting the Controller. [10]

Model 2 MVC framework has been greatly appreciated by the industry. In Python, frameworks like Django [11] have used it. And, of course, in the ASP.NET world, frameworks like ASP.NET MVC [12], Maverick .NET [13] etc have used it.

There are many frameworks in Ruby which implement MVC. Some of them are Monkeybars, Camping, Nitro, and Ruby on Rails etc. Ruby on Rails is the most widely know MVC framework pattern and will be easy to understand due to our previous background.[14]. Ruby on Rails provides special features to implement MVC framework. It enables you to create the MVC file structure easily. It is divided into separate packages which gets included by the model, view and controller files automatically to connect the functionalities of these files.

  • Model (ActiveRecord) : The ActiveRecord package maintains all the library functions which are required for binding the interface (i.e. object) and database. It also helps in handling validation, association, transactions, and more. Thus inclusion of this package enables Model to implement its functions.
  • View (ActionView) : The ActionView package implements Embedded Ruby(ERb) based system. These help to define templates for data presentation. By including this library, the view files can use scripting languages like JSP, ASP, and PHP to display the data. It also helps to integrate AJAX technology with these files.
  • Controller (ActionController): The ActionController package is included in a controller file by default when it is created. This gives default controller behavior to it. It helps to organize the data by searching and sorting so that the view does not have any calculations to perform.ActionController package acts like a bridge between ActiveRecord (the database interface) and ActionView (the presentation engine).

Thus ActionController and ActionView provide facilities for displaying the data and ActionRecord provides techniques for manipulating database.

Flex

Cairngnorm is considered to be the best practice methodology for Flex software design [15]

JAVA

There are many frameworks like Struts, Spring which adopt the MVC methodology and are pertaining to JAVA.


PHP

It has a plethora of MVC implementations. The top 10 MVC-based PHP frameworks are

  1. Symfony: Based on Mojavi and inspired by Rails
  2. Mojavi
  3. CakePHP: Inspired by Rails PHP4/5
  4. PHPOnTrax: a Rails port - PHP5 Only
  5. Prado: The winner of Zend coding contest
  6. Studs: A Java-Struts port to PHP
  7. Phrame: A Java-Struts port
  8. Achievo: A good RAD framework
  9. WACT: Web Application Component Toolkit
  10. Ambivalence: A Java-Maverick Port

References