CSC/ECE 517 Fall 2009/wiki1b 13 za: Difference between revisions
No edit summary |
|||
(102 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
= History and Applications of the MVC Pattern = | =History and Applications of the MVC Pattern= | ||
== MVC - the ''buzz'' term! == | |||
[[Image:mvc.png|thumb|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 a widely used architecture for web-based applications. 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 well-defined strategy 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 the interactions between the various modules. What is achieved by keeping these functionalities separate is ''reusability'' which is one of the major advantages of MVC. | |||
== Examples on MVC == | |||
{| class="wikitable" border="1" | |||
|- | |||
! Title | |||
! Model | |||
! View | |||
! Controller | |||
|- | |||
| Messageboard application | |||
| The back-end of the database, where the messages are stored | |||
| Displaying the message board discussions on a browser | |||
| Functionality for posting a new topic, or editing a topic already posted | |||
|- | |||
| Shopping Cart Application | |||
| The Shopping Cart itself that stores information about products purchased | |||
| The webpage that maintains a list of products, quantities, etc. and gets updated with changes made by the customer | |||
| The business logic that triggers actions to manipulate the data for the cart - generate bill, remove items from cart | |||
|- | |||
| Weather | |||
| Thermometer that performs the basic function of recording current temperature | |||
| Display of temperature readings | |||
| Sensor in the thermometer that is capable of recording the current temperature at any given instant of time | |||
|- | |||
| Banking System | |||
| User Account | |||
| User login display, different types of account related to a user, list various options for an account, displaying the recent few transactions | |||
| Functionality for triggering actions like - logging in, transferring amount, filtering the list of transactions, calculating the interest etc. | |||
|- | |||
| A Computer | |||
| The Operating System can be considered as the underlying model of any computer | |||
| The output device - monitor | |||
| The central processing unit that arbitrates various computations or operations to be performed | |||
|} | |||
''Above examples have been adapted from the discussion in CSC517 course'' | |||
== Evolution of MVC == | |||
=== MVC Architecture === | |||
Over all these years, history of MVC architecture has become dim. MVC was first modeled at PARC company (Palo Alto Research Center, formerly called 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 [http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html]. Even though they changed his implementation and removed the 'Editor', Trygve Reenskaug was credited for his work. | |||
=== MVP Architecture === | |||
After the implementation of Smalltalk-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 to MVC controller. The only difference was in the way the presenter was linked to the associated views. But over time, the difference got more obscure and now implementations of MVC frameworks follow more concepts of MVP [http://www.typo3-media.com/blog/mvc-mvp-typo3-introduction.html]. | |||
=== 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 [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]. More information on the use of MVC as an architectural pattern or a design pattern can be found at [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller] | |||
== 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 [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller#Implementations_of_MVC_as_web-based_frameworks]. All the implementations of MVC fall into three categories: Oringinal MVC framework, Model 2 MVC framework and Web application framework. | |||
=== Original MVC === | |||
[[Image:MVC2.jpg|thumb]] | |||
The original MVC pattern is very simple and has a very different path for the data, request and response of the user. Here the Controller modifies 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" [http://www.martinfowler.com/eaaDev/uiArchs.html]. | |||
Original MVC Model is very simple to understand and implement. It was the early approach for web-application development but is still used for simple applications. 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 the implementation is easy, work ''cannot'' be divided. | |||
Not many web-based frameworks use this approach. JavaServer Pages (JSP) uses this approach. This approach is also called page-centric approach. Here we only build JSP pages which interact with users and adopt 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. After all the processing is done and the content is generated dynamically, it is displayed back to the user [http://tutorials.beginners.co.uk/professional-jsp-architecture-part-4-the-page-centric-approach.htm]. | |||
=== Model 2 MVC Framework === | |||
[[Image:MVC3.jpg|thumb]] | |||
Most of the web-based implementations follow this version of the MVC framework. Any present MVC framework is considered synonymous with this model. In this model, there is no more contact between 'Model' and 'View'. Everything happens with the 'Controller' at the center. Controller is responsible for co-ordinating the tasks within an application. | |||
Whenever a request arrives, it first goes to Controller, then it passes the data to Model for validation and processing. Then if the Model needs to pass some data back to the View, it does so, through the Controller, which invokes the View. | |||
Model 2 MVC framework helps to create complex applications easily. This approach is used for web-based applications that need to be modified regularly and maintained for a long time. Because of this clean separation between the three components, it becomes easy for a development team to work on an application by adopting a modular approach. In other words, if one group in the Development team works on the 'View' of the application, another group can work on the implementation details of the 'Model'. [http://stephenwalther.com/blog/archive/2008/08/24/the-evolution-of-mvc.aspx] | |||
Model 2 MVC framework has been greatly appreciated by the industry. In Python, frameworks like Django [http://en.wikipedia.org/wiki/Django_web_framework] have used it. And, of course, in the ASP.NET world, frameworks like ASP.NET MVC [http://en.wikipedia.org/wiki/ASP.NET_MVC_Framework], Maverick .NET [http://en.wikipedia.org/wiki/Maverick.NET] 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 known MVC framework pattern and will be easy to understand due to our previous background.[http://www.tutorialspoint.com/ruby-on-rails/rails-framework.htm]. 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 that get included by the Model, View and Controller files automatically. | |||
* 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. 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 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 the database. | |||
=== Web Application Framework === | |||
After the Model 2 MVC framework became popular, it was enhanced further for its use on enterprise-based web applications. JAVA, PHP, Flex tools and the like became highly popular with their applications in web development. These technologies came up with their own MVC frameworks. A few well-known web application MVC frameworks are Struts and Spring (JAVA based), Cairngorm (Flex), Symfony Framework (PHP). All these frameworks are typically based on MVC Model 2, but have added a rich set of features and unit-testing that facilitate easy maintenance of the web-application. | |||
Struts from Apache is a an open source MVC based framework for creating JAVA applications [http://struts.apache.org/] | |||
The framework provides three key components: | |||
* There is a central configuration file struts-config.xml that holds together the model, view and controller. | |||
* Controller: The controller is a Servlet called as ActionServlet. | |||
* View: Typically involves using JSP pages. | |||
Requests from the client are sent to the controller in the form of "Actions" defined in the configuration file; if the controller receives such a request it calls the corresponding Action class that interacts with the application-specific model code. The model code returns an "ActionForward", a string telling the controller that output page needs to be sent to the client. Information is passed between model and view in the form of special JavaBeans. A powerful custom tag library allows it to read and write the content of these beans from the presentation layer without the need for any embedded Java code. [http://en.wikipedia.org/wiki/Apache_Struts] | |||
= Conclusion = | |||
This wiki thus throws light on what is a MVC pattern, how MVC evolved since its first proposal by Reenskaug, and how it is still evolving in the present in terms of the framework specifications pertaining to different forms of programming languages. MVC architecture, MVP architecture, MVC design pattern are the variants of MVC pattern that were modeled in the path of its evolution. And finally, we highlighted how MVC got differentiated into Model 1 MVC, Model 2 MVC and the most recent form: Web-based MVC framework that highly dwells on the Model 2 version. | |||
= References = | |||
# [http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html Trygve Reenskaug's early document on MVC] | |||
# [http://www.typo3-media.com/blog/mvc-mvp-typo3-introduction.html MVC: Introduction and History] | |||
# [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Wikipedia page on 'Design Pattern'] | |||
# [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Wikipedia page on 'MVC'] | |||
# [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller#Implementations_of_MVC_as_web-based_frameworks Wikipedia page on 'Implementation of MVC as web-based frameworks'] | |||
# [http://www.martinfowler.com/eaaDev/uiArchs.html MVC and MVP] | |||
# [http://tutorials.beginners.co.uk/professional-jsp-architecture-part-4-the-page-centric-approach.htm JSP: The page-centric approach] | |||
# [http://stephenwalther.com/blog/archive/2008/08/24/the-evolution-of-mvc.aspx Evolution of MVC] | |||
# [http://en.wikipedia.org/wiki/Django_web_framework Wikipedia page on 'Django web framework'] | |||
# [http://en.wikipedia.org/wiki/ASP.NET_MVC_Framework Wikipedia page on 'ASP.NET web framework'] | |||
# [http://en.wikipedia.org/wiki/Maverick.NET Wikipedia page on 'Maverick.NET'] | |||
# [http://www.tutorialspoint.com/ruby-on-rails/rails-framework.htm Ruby on Rails framework tutorial] | |||
# [http://struts.apache.org/ Struts framework] | |||
# [http://en.wikipedia.org/wiki/Apache_Struts Wikipedia page on Apache Struts] |
Latest revision as of 18:28, 29 September 2009
History and Applications of the MVC Pattern
MVC - the buzz term!
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 a widely used architecture for web-based applications. 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 well-defined strategy 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 the interactions between the various modules. What is achieved by keeping these functionalities separate is reusability which is one of the major advantages of MVC.
Examples on MVC
Title | Model | View | Controller |
---|---|---|---|
Messageboard application | The back-end of the database, where the messages are stored | Displaying the message board discussions on a browser | Functionality for posting a new topic, or editing a topic already posted |
Shopping Cart Application | The Shopping Cart itself that stores information about products purchased | The webpage that maintains a list of products, quantities, etc. and gets updated with changes made by the customer | The business logic that triggers actions to manipulate the data for the cart - generate bill, remove items from cart |
Weather | Thermometer that performs the basic function of recording current temperature | Display of temperature readings | Sensor in the thermometer that is capable of recording the current temperature at any given instant of time |
Banking System | User Account | User login display, different types of account related to a user, list various options for an account, displaying the recent few transactions | Functionality for triggering actions like - logging in, transferring amount, filtering the list of transactions, calculating the interest etc. |
A Computer | The Operating System can be considered as the underlying model of any computer | The output device - monitor | The central processing unit that arbitrates various computations or operations to be performed |
Above examples have been adapted from the discussion in CSC517 course
Evolution of MVC
MVC Architecture
Over all these years, history of MVC architecture has become dim. MVC was first modeled at PARC company (Palo Alto Research Center, formerly called 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 [1]. Even though they changed his implementation and removed the 'Editor', Trygve Reenskaug was credited for his work.
MVP Architecture
After the implementation of Smalltalk-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 to MVC controller. The only difference was in the way the presenter was linked to the associated views. But over time, the difference got more obscure and now implementations of MVC frameworks follow more concepts of MVP [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 [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 [5]. All the implementations of MVC fall into three categories: Oringinal MVC framework, Model 2 MVC framework and Web application framework.
Original MVC
The original MVC pattern is very simple and has a very different path for the data, request and response of the user. Here the Controller modifies 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" [6].
Original MVC Model is very simple to understand and implement. It was the early approach for web-application development but is still used for simple applications. 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 the implementation is easy, work cannot be divided.
Not many web-based frameworks use this approach. JavaServer Pages (JSP) uses this approach. This approach is also called page-centric approach. Here we only build JSP pages which interact with users and adopt 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. After all the processing is done and the content is generated dynamically, it is displayed back to the user [7].
Model 2 MVC Framework
Most of the web-based implementations follow this version of the MVC framework. Any present MVC framework is considered synonymous with this model. In this model, there is no more contact between 'Model' and 'View'. Everything happens with the 'Controller' at the center. Controller is responsible for co-ordinating the tasks within an application.
Whenever a request arrives, it first goes to Controller, then it passes the data to Model for validation and processing. Then if the Model needs to pass some data back to the View, it does so, through the Controller, which invokes the View.
Model 2 MVC framework helps to create complex applications easily. This approach is used for web-based applications that need to be modified regularly and maintained for a long time. Because of this clean separation between the three components, it becomes easy for a development team to work on an application by adopting a modular approach. In other words, if one group in the Development team works on the 'View' of the application, another group can work on the implementation details of the 'Model'. [8]
Model 2 MVC framework has been greatly appreciated by the industry. In Python, frameworks like Django [9] have used it. And, of course, in the ASP.NET world, frameworks like ASP.NET MVC [10], Maverick .NET [11] 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 known MVC framework pattern and will be easy to understand due to our previous background.[12]. 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 that get included by the Model, View and Controller files automatically.
- 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. 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 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 the database.
Web Application Framework
After the Model 2 MVC framework became popular, it was enhanced further for its use on enterprise-based web applications. JAVA, PHP, Flex tools and the like became highly popular with their applications in web development. These technologies came up with their own MVC frameworks. A few well-known web application MVC frameworks are Struts and Spring (JAVA based), Cairngorm (Flex), Symfony Framework (PHP). All these frameworks are typically based on MVC Model 2, but have added a rich set of features and unit-testing that facilitate easy maintenance of the web-application.
Struts from Apache is a an open source MVC based framework for creating JAVA applications [13]
The framework provides three key components:
- There is a central configuration file struts-config.xml that holds together the model, view and controller.
- Controller: The controller is a Servlet called as ActionServlet.
- View: Typically involves using JSP pages.
Requests from the client are sent to the controller in the form of "Actions" defined in the configuration file; if the controller receives such a request it calls the corresponding Action class that interacts with the application-specific model code. The model code returns an "ActionForward", a string telling the controller that output page needs to be sent to the client. Information is passed between model and view in the form of special JavaBeans. A powerful custom tag library allows it to read and write the content of these beans from the presentation layer without the need for any embedded Java code. [14]
Conclusion
This wiki thus throws light on what is a MVC pattern, how MVC evolved since its first proposal by Reenskaug, and how it is still evolving in the present in terms of the framework specifications pertaining to different forms of programming languages. MVC architecture, MVP architecture, MVC design pattern are the variants of MVC pattern that were modeled in the path of its evolution. And finally, we highlighted how MVC got differentiated into Model 1 MVC, Model 2 MVC and the most recent form: Web-based MVC framework that highly dwells on the Model 2 version.
References
- Trygve Reenskaug's early document on MVC
- MVC: Introduction and History
- Wikipedia page on 'Design Pattern'
- Wikipedia page on 'MVC'
- Wikipedia page on 'Implementation of MVC as web-based frameworks'
- MVC and MVP
- JSP: The page-centric approach
- Evolution of MVC
- Wikipedia page on 'Django web framework'
- Wikipedia page on 'ASP.NET web framework'
- Wikipedia page on 'Maverick.NET'
- Ruby on Rails framework tutorial
- Struts framework
- Wikipedia page on Apache Struts