Friday, April 2, 2010

Workshop 2 Model View Controller design approach

Workshop 2 Model View Controller design approach

Topic objectives

• Describe the history and architecture of the Model View Controller (MVC) approach to Web application design;
• Revise database techniques with Rails and SQL
• Describe how to use MVC in the Ruby on Rails development environment


Workshop 2 further emphasises the MVC approach to web apprlication design.and also revisits the Convention over configuration approach that is adopted by Ruby on Rails. To reiterate and build on the work that has already been covered in Workshop 1 and previous exercises, convention over configuration is a software design paradigm which seeks to reduce the number of decisions that developers need to make, gaining simplicity without necessarily losing flexibility. Badly designed frameworks which do not follow this convention often need multiple configuration files, each with many settings. Obviously this increases the amount of coding required. By adopting the convention over configiration approach, a developer only needs to specify unconventional aspects of the application. For example, if there is a class Sale in the model, the corresponding table in the database is called sales by default. It is only if one deviates from this convention, such as calling the table "products sold", that the developer needs to write code regarding these names. Generally, this leads to less code and less repetition.


In order to further develop my understanding of MVC I visited the the "Understanding MVC" wiki link provided in the workshop (http://wiki.rubyonrails.org/rails/pages/UnderstandingMVC). However this lik did not work. I then took the MVC Tutorial . This tutorial provided a relatively elemntary overview of MVC, revisiting information that has already been present in previous posts. I then completed the RAD lab’s Ruby on Rails Short course. Thi video provide a quick overview of HTML, HTTP, Web 1.0 Apps and a good introduction to Ruby on Rails including the installation and basic folder structure and the running of a basic application (Hello World) in order to understand the basic inner workings of Ruby on Rail. There was also some brief discussion on the MVC design architecture. Finally I read Read the Flash article using ActionScript by Colin Moock titled The Model-View-Controller Design Pattern. This article also reiterated the information on MVC that had been covered previously.


Overall this workshop provided a good look at how not only Rails applications are structured but also how other frameworks are utilising the MVC architecture. This workshop also presented two Challenge problems, which are presented and addressed below.


Challenge Problems:


1. How is Rails structured to follow the MVC pattern?

Consider our project and examine the directories where Rails is located. If the data model is called Taxi (it is convention to name the model beginning with an upper case letter). The model is a Ruby class located in app/models/taxi.rb


The SQL table is taxis – the pluralisation of the model. In our project we have 2 tables as passenger_origin and passenger_destination, where the table row = an object instance and each of the columns = an object attribute.


The controller methods live in app/controllers/taxi_controller.rb

Each controller can access templates to display the input screen and methods for action.


The views are kept is app/views/taxi/*.rhtml, where each *.rhtml maps to a controller method.

In Rails, the view is rendered using RHTML or RXML. According to the wiki page at http://wiki.rubyonrails.org/rails/pages/UnderstandingViews, RHTML is HTML with embedded Ruby code and RXML is Ruby-generated XML code.


I had significant issues in trying to determine what this challenge problem was actually asking, as it appears to be making statements rather asking questions. I took this challenge as simply prompting the student to start thinking about the up and coming Taxi Booking System application that will be debeveloped and examined throughout this workshop. The Rails framework is structured to follow the MVC pattern by providing code generation techniques that build each component of the pattern. As such I started building this application by starting Instant Rails and running the "Rails" command to create the basic template for the taxi app. I typed "rails taxi" at the command prompt, and some seconds later, Ruby on Rails had created the basic directory structure for my application, as can be seen in the below image.



The next logical steps were then to set up the initial Model, View and Controller components of the application using the relevant rails commands. I changed directy in the Ruby command prompt to the newly created "taxi" directory.

To set up the Model component I used the Command "ruby script/generate model taxi". To set up the Controller component I used the Command "ruby script/generate controller taxi". To build the view component I chose to use the scaffolding feature in Ruby which quickly creates a web interface for interacting with your model data. This was done using the command "ruby script/generate scaffold". Through this easy series of commands the basic structure and data has been created for the beginnings of the taxi application.


2. Apply the MVC design approach to our Project: Online Taxi Booking System.

HINT: Begin with a single model, single view and single controller classes. This will give you a head start to the next workshop: Online Taxi Booking System: SQL and Database design


By following the process outlined above in Challenge 1 the MVC design approach was utilised to create the basic application framework for the Taxi application including the general taxi directory, the taxi model using the Ruby model generator, the taxi controller using the Ruby controller generator and the Taxi model using the Ruby scaffolding genrator.




No comments:

Post a Comment