CSC/ECE 517 Fall 2009/wiki1b 13 pz

From Expertiza_Wiki
Revision as of 01:32, 20 September 2009 by Plants (talk | contribs)
Jump to navigation Jump to search

Model–View–Controller (MVC) is an architecture pattern to separate the business logic of an application from the Graphical User Interface (GUI), so we could modify either of the two without affecting the other one. And programs with MVC is highly maintainable, which could save a lot of work in the future usage, though it may be time-consuming when first coded.

History

MVC was invented at Xerox Parc in the 70's, first introduced in a paper “A Cookbook for Using the Model-View-Controller User Interface Paradigm in Smalltalk -80”, by Glenn Krasner and Stephen Pope, published in the August/September 1988 issue of the Journal Of Object Oriented Programming (JOOP). Due to its big advantage of better organization and code reusability, it’s getting more and more popular. Nowadays, MVC is used on ruby on rails, ASP.NET, Sun’s J2EE platform, ColdFusion and PHP developers etc.

Headline text

Introduction to MVC

The Relationship between Model, View and Controller

Model–view–controller(MVC)obviously is consist of three parts: model, view and controller. Each of them is somehow independent from each other; meanwhile they have some kind of connections with each other.

  • Model is the main logic domain of an application, storing data and defining means of how to change and process the data. The state of model can be access under the request by view, and can be change by the controller. One model could have multiple views and controllers.
  • View presents the state of model and data to the outer world, and could be changed by the controller. View could access the data of model freely; however, it could not change the model. The view should change when the corresponding model changes.
  • Controller is the mean for the user to control the application. The controller acts as the input to the application, it changes the model and as the user requests. The controller is also responsible for creating and selecting views which sometimes is delegated to a specific object, this is known as the Application Controller pattern for web MVC and View Handler for GUI MVC.



Applications of MVC

Ruby

The Rails Application Architecture

In Ruby on Rails, the application’s work is divided into three parts which are separate but cooperative by the Model-View-Controller pattern.

  • ActiveRecord (Model): The Model part is built in ActiveRecord library which provides an interface and binds together the tables in a relational database and the Ruby program code which manipulates database records. The field names of database tables can automatically generate Ruby method names, and so on.
  • ActionView (View): The View is built in ActionView library. This library is an Embedded Ruby based system for data presentation by providing corresponding templates. If there is a Web connection to the Rails application, it will result in the displaying of a view.
  • ActionController (Controller): The Controller part is built in ActionController. It is a data agent to translate interactions with ActionView which is the presentation engine into actions to be performed by ActiveRecord which is the database interface.

In summary, ActiveRecord provides a range of programming methods and paths for manipulating data from an SQL database. ActionController and ActionView provide the platform for controlling and displaying that data. Rails is responsible for tying them all together. The cookbook of Ruby on Ralis can be a good example for this.

Java

In Java platform, the following Java technologies can be applied to various components of the Model-View-Controller pattern.

  • Model: The Model part can be implemented using the entity bean and session bean. It also can be implemented by a Java Servlet using a business object framework such as Spring.
  • View: The View part can be created by a JavaServer Page (JSP) using JavaServer Faces (JSF) Technology.
  • Controller: The Controller part can be simply implemented by a Java Servlet.

Here is an example that a web application allows the user to view and edit the data in a table named “Users” in a db file.

Table: Users
The columns: ID, Last Name, First Name
Insert into table one data row in order to have at least one default user.

Suppose a persistence provider like hibernate is used. For this element (users table), entity bean can be created. A session bean can be created with EntityManager which realizes the methods “create”, "edit", "find" and “delete”.

 create – creates new table entities
 edit - processes the update users using the EntityManager method merge.
 find - returns the entity bean that represents the data in the table with id indicated
 delete – erases table entities

If this portion is implemented, it will get the Model part.

A servlet (servlet user) is implemented which calls through resource injection and the session bean. Then by invoking this servlet model and using the method find(x), the equivalent entity containing the data with “id = x” can be obtained.

If the servlet receives the GET or POST parameter "ID", "Last Name" and "First Name", it will perform the db upgrade by creating a new entity with the data passed through the parameters and then calling the method of the “edit” session bean. The Controller part will realize the servlet in this case.

The servlet article uses a RequestDispatcher which can forward the request and data to a JSP which is responsible to show a html form which allows to read or edit the data. This is the View part.