CSC/ECE 517 Fall 2012/ch1a 1w12 mv: Difference between revisions
Line 41: | Line 41: | ||
With the advancement in technologies such as network, processors, browsers, thin client, software development has moved from a desktop application model to a web application/web services mode. This paradigm shift has caused a need for better server side and client side frameworks that aid in faster application development. Consequently, Scaffolding is now a part of many web application frameworks. We can find a number of development software’s that uses web application scaffolding approach. Frameworks include, | With the advancement in technologies such as network, processors, browsers, thin client, software development has moved from a desktop application model to a web application/web services mode. This paradigm shift has caused a need for better server side and client side frameworks that aid in faster application development. Consequently, Scaffolding is now a part of many web application frameworks. We can find a number of development software’s that uses web application scaffolding approach. Frameworks include, | ||
Ruby on Rails, | |||
CakePHP , | |||
ASP.NET | |||
CodeIgniter | |||
iPhone Web Scaffolding | |||
Grails | |||
These frameworks actually contain some software components that create the skeletal attachments to the application’s database and other external devices. | These frameworks actually contain some software components that create the skeletal attachments to the application’s database and other external devices. | ||
There are primarily two kinds of scaffolding that are available as a part of web application today, | There are primarily two kinds of scaffolding that are available as a part of web application today, | ||
1) Dynamic Scaffolding | 1) Dynamic Scaffolding | ||
Dynamic scaffold works by dynamically generating all the views and controllers | Dynamic scaffold works by dynamically generating all the views and controllers | ||
at runtime so the user won't actually see any new views or controllers as part of their | at runtime so the user won't actually see any new views or controllers as part of their | ||
application. It'll also let you make minor customizations to that user interface | application. It'll also let you make minor customizations to that user interface | ||
Line 56: | Line 58: | ||
2) Static Scaffolding | 2) Static Scaffolding | ||
Static scaffolding works by generating actual views and controllers from the command line | Static scaffolding works by generating actual views and controllers from the command line | ||
so that one can actually change the logic of the scaffolding via the controllers or change the look and feel of it through the views that it generates. Static scaffolding is quite a bit more flexible than dynamic scaffolding. | so that one can actually change the logic of the scaffolding via the controllers or change the look and feel of it through the views that it generates. Static scaffolding is quite a bit more flexible than dynamic scaffolding. | ||
We will see how scaffolding works in various web application frameworks. | |||
== Scaffolding in MVC based Web Frameworks == | == Scaffolding in MVC based Web Frameworks == |
Revision as of 23:15, 14 September 2012
Overview
Scaffolding in General
Scaffold is a temporary platform, either supported from below or suspended from above, on which workers sit or stand when performing tasks at heights above the ground. Because of its nature of its use, it must made sure that it is properly constructed and ensures the safety of those who use it.
Scaffolding in Programming
Deriving from the analogy above, scaffolding in web application framework refers to providing a minimal setup for various components that make a web application. These components include but are not limited to databases, application servers and web servers. Such scaffolds are generally standard, bare minimal and reusable components that are automatically generated.
A typical web application contains a database layer for retrieval and storage of information. The basic operations performed to access the database are grouped into the following types, which are commonly referred to as the CRUD functionality.
Create Read Update Delete
A scaffold for web application will typically provide stubs for performing the above mentioned CRUD functions. This boilerplate code provides the essential infrastructure to develop a web application in quick time.
MVC framework is a popular web application framework that separates concerns and applications into model, view and controllers. In MVC, scaffolding will usually create basic model, views and controllers and also the database objects needed.
History of Scaffolding
Origin and Evolution
Scaffolding Theory
Scaffolding theory in education came into being in 1950, by, Jerome Bruner, where he described the various scaffolding techniques for oral language and written vocabulary. In Oral scaffolding, parents and adults help the children to make them know how to speak and communicate, how they should develop the base or a temporary framework for their children to walk in the new world. Similarly, in written scaffolding, typical supporting instructions were given by the instructor to the students to learn vocabulary, to calibrate the task, to identify the task and work independently on the task, in this way they built the scaffold for them on which they can lean on while learning. Scaffolding Origin in Web Applications Scaffolding has been around in various forms for some time. Earlier, certain development environment included application code module generators such as Oracle's CASE generators. They were often the third party generators which offered varying capabilities and mainly acted as database code generators. One could also have custom scripts, which could generate the CRUD screens from some database specifications. Often each new project involved writing up some templates to get the basic functionality or writing a template generator script, which created these templates for a project. But these generators and scripts were non-standardized and often viewed as taking time away from the core development of an application. In order to aid in faster application development and also standardize the code generation, the concept of scaffolding were introduced in web application frameworks.
What Scaffolding is Today?
Current Scaffolding Scope
With the advancement in technologies such as network, processors, browsers, thin client, software development has moved from a desktop application model to a web application/web services mode. This paradigm shift has caused a need for better server side and client side frameworks that aid in faster application development. Consequently, Scaffolding is now a part of many web application frameworks. We can find a number of development software’s that uses web application scaffolding approach. Frameworks include, Ruby on Rails, CakePHP , ASP.NET CodeIgniter iPhone Web Scaffolding Grails These frameworks actually contain some software components that create the skeletal attachments to the application’s database and other external devices.
There are primarily two kinds of scaffolding that are available as a part of web application today,
1) Dynamic Scaffolding
Dynamic scaffold works by dynamically generating all the views and controllers
at runtime so the user won't actually see any new views or controllers as part of their application. It'll also let you make minor customizations to that user interface via settings in the model itself.
2) Static Scaffolding
Static scaffolding works by generating actual views and controllers from the command line
so that one can actually change the logic of the scaffolding via the controllers or change the look and feel of it through the views that it generates. Static scaffolding is quite a bit more flexible than dynamic scaffolding.
We will see how scaffolding works in various web application frameworks.
Scaffolding in MVC based Web Frameworks
MVC is a design paradigm that originated from user interface development. MVC basically deals with the separations of concern between model, view and business logic. Several commercial and non commercial application framework have been created to enforce the design. Rails also uses MVC framework to implement web applications, and scaffolding helps to generate this model-view-controller code that we see in detail. First we will describe how the scaffolding approach works in Rails framework. Scaffolding gets its popularity with the rails framework only, and it comes as the inbuilt feature of the rails framework. Scaffolding in Rails Framework Rails implements static scaffolding, where the user specifies the command " scaffold Post title: string content:text category_id: integer" to generate the scaffold. This creates model, view and controller for the entity "Post". The user can change the functionality of these components.
Now lets see how scaffolding in the rails framework helps to create the database schema at the start of an application by using very simple commands. Follow these steps in the Eclipse IDE to create a scaffolding of a car website. We just define some attributes of a car like its color, year and model. Step -1. Create New Rails Project: New->New Rails Project Step -2. Get the generator view: Window->Show View->Generators Step -3. Make sure current project for Generator is the project that you want. Step -4. Select the scaffold generator from drop down box: Generator->scaffold Step -5. In parameters, specify the model, fields and their type Here we take, Car color:string maker:string year:date Step -6. This will run the generate script and create files in View, controllers, model, db etc as shown below:
>script/generate scaffold Car color:string maker:string model:number year:date
exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/cars exists app/views/layouts/ exists test/functional/ exists test/unit/ exists test/unit/helpers/ exists public/stylesheets/ create app/views/cars/index.html.erb create app/views/cars/show.html.erb create app/views/cars/new.html.erb create app/views/cars/edit.html.erb create app/views/layouts/cars.html.erb create public/stylesheets/scaffold.css create app/controllers/cars_controller.rb create test/functional/cars_controller_test.rb create app/helpers/cars_helper.rb create test/unit/helpers/cars_helper_test.rb route map.resources :cars dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/car.rb create test/unit/car_test.rb create test/fixtures/cars.yml create db/migrate create db/migrate/20100919144829_create_cars.rb
The framework automatically generates all the database models and code for rendering the views. However at this point the database tables not exist.
It also creates a file containing the schema for the model requested by the user as shown below. class CreateCars < ActiveRecord::Migration def self.up create_table :cars do |t| t.string :color t.string :maker t.number :model t.date :year t.timestamps end end
def self.down drop_table :cars
end end Step -7 You then run a rake task to actually create the database tables using the migrate file. Step -8 Now go to Window->Show View->Rake Step -9 In the Rake window, select migrate task from the drop down box and run it. You can also right click the create_cars.rb file, select the rake menu option and then select migrate option.
rake db:migrate
This command would actually create the tables defined in the schema. Step -10 You can then go to your sqlite database and check that tables have been created. Scaffold will always generate the tables in the Development database. With this you now have a functioning application. Start the web server and point your browser to http://localhost:3000/cars(unless you decided to override the defaults). You will be presented with a basic screen showing all the cars and their details. Links to web pages to add, edit and destroy a record are also provided. Using these, you can modify existing records or add new records. Sample auto generated code for the above example is as below. Controller for car in cars_controller.rb class CarsController < ApplicationController
# GET /cars # GET /cars.xml def index @cars = Car.all
respond_to do |format| format.html # index.html.erb format.xml { render :xml => @cars } end end
# GET /cars/1 # GET /cars/1.xml def show @car = Car.find(params[:id])
respond_to do |format| format.html # show.html.erb format.xml { render :xml => @car } end end
# GET /cars/new # GET /cars/new.xml def new @car = Car.new
respond_to do |format| format.html # new.html.erb format.xml { render :xml => @car } end end
# GET /cars/1/edit def edit @car = Car.find(params[:id]) end
# POST /cars # POST /cars.xml def create @car = Car.new(params[:car])
respond_to do |format| if @car.save format.html { redirect_to(@car, :notice => 'Car was successfully created.') } format.xml { render :xml => @car, :status => :created, :location => @car } else format.html { render :action => "new" } format.xml { render :xml => @car.errors, :status => :unprocessable_entity } end end end
# PUT /cars/1 # PUT /cars/1.xml def update @car = Car.find(params[:id])
respond_to do |format| if @car.update_attributes(params[:car]) format.html { redirect_to(@car, :notice => 'Car was successfully updated.') } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @car.errors, :status => :unprocessable_entity } end end end
# DELETE /cars/1 # DELETE /cars/1.xml def destroy @car = Car.find(params[:id]) @car.destroy
respond_to do |format| format.html { redirect_to(cars_url) } format.xml { head :ok } end end
end As seen above the controller.rb files contain the controller generated through the scaffolding process. The user is allowed to make changes to the controller to customize the application to his or her needs. Sample view file for EDIT generated in views->cars->edit.html.erb
Editing car
<% form_for(@car) do |f| %>
<%= f.error_messages %>
<%= f.label :color %>
<%= f.text_field :color %>
<%= f.label :maker %>
<%= f.text_field :maker %>
<%= f.label :model %>
<%= f.text_field :model %>
<%= f.label :year %>
<%= f.dat
Headline text
e_select :year %>
<%= f.submit 'Update' %>
<% end %>
<%= link_to 'Show', @car %> | <%= link_to 'Back', cars_path %>
Similar to the controllers, the views can also be edited to allow customization.
Dynamic Scaffolding in Grails
Grails supports dynamic and static scaffolding. As we have already seen static scaffolding, we will see how dynamic scaffolding is implemented in grails with a single statement "static scaffold =true". The simplest way to get started with scaffolding is to enable it with the scaffold property. Set the scaffold property in the controller to true for the Book domain class: class BookController {
static scaffold = true
} This works because the BookController follows the same naming convention as the Book domain class. To scaffold a specific domain class we could reference the class directly in the scaffold property: class SomeController {
static scaffold = Author
} With this configured, when you start your application the actions and views will be auto-generated at runtime. The following actions are dynamically implemented by default by the runtime scaffolding mechanism: • list • show • edit • delete • create • save • update A CRUD interface will also be generated. To access this open http://localhost:8080/app/book in a browser. You can add new actions to a scaffolded controller, for example: class BookController {
static scaffold = Book def changeAuthor() { def b = Book.get(params.id) b.author = Author.get(params["author.id"]) b.save() // redirect to a scaffolded action redirect(action:show) }
} You can also override the scaffolded actions: class BookController {
static scaffold = Book // overrides scaffolded action to return both authors and books def list() { [bookInstanceList: Book.list(), bookInstanceTotal: Book.count(), authorInstanceList: Author.list()] } def show() { def book = Book.get(params.id) log.error(book) [bookInstance : book] }
} All of this is what is known as "dynamic scaffolding" where the CRUD interface is generated dynamically at runtime.
Other Examples
Apache Tapestry is an open-source framework for creating dynamic, robust, highly scalable web applications in Java. Tapestry complements and builds upon the standard Java Servlet API, and so it works in any servlet container or application server. Tapestry includes some scaffolding components that generate code at runtime thus allowing an application to be dynamically assembled at runtime. The two main scaffolding components are BeanEditForm - A component that creates an entire form editing the properties of a particular bean. It generates a simple UI for editing the properties of a JavaBean, with the flavor of UI for each property (text field, checkbox, drop down list) determined from the property type (or by other means, such as an annotation), and the order and validation for the properties determined from annotations on the property's getter and setter methods. Grid - A grid presents tabular data. It is a composite component, created in terms of several sub-components. The sub-components are statically wired to the Grid, as it provides access to the data and other models that they need. A Grid may operate inside a form. http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/BeanEditForm.html http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/Grid.html A more detailed listing of examples of scaffolding in various other web frameworks can be found in last year's treatment of the same topic here. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki1b_9_ss
Conclusion
Thus we have discussed scaffolding in general and how it was realized for web application development. We also discussed the various types of scaffolding and how they are implemented in some of the popular web application frameworks. These bare minimal, reusable, and standard code make web application development a easy task.
See Also
You can go through some of the other frameworks to create scaffold web applications. 1. CakePHP Framework 2. Grails Framework 3. ASP.NET Framework 4. Codeigniter 5. iPhone 6. NetBeans CRUD Generator
Bibiliography
References
1. Programming Ruby(2nd Edition): The Pragmatic Programmers' Guide (Pragmatic Programmers Series), by Dave Thomas, Andy Hunt, Andrew Hunt, Chad Fowler, Chad Fowler: Publisher: Pragmatic Bookshelf 2. Agile Web Development with Rails, 3rd ed. by Dave Thomas, David Heinemeier Hansson, David Hansson, Publisher: Pragmatic Bookshelf 3. http://msdn.microsoft.com/en-us/library/cc488469.aspx 4. http://guides.rubyonrails.org/getting_started.html
External Sites
1. http://en.wikipedia.org/wiki/Scaffolding_Theory 2. http://www.wisegeek.com/what-is-web-application-scaffolding.htm 3. http://www.developer.com/lang/php/article.php/3636686/Scaffolding-with-CakePHP---Managing-Your-Fantasy-Football-Team.htm 4. http://cpan.uwinnipeg.ca/htdocs/Scaffold/Scaffold.html
MediaSite
1. http://www.asp.net/aspnet-in-net-35-sp1/videos/getting-started-with-dynamic-data 2. http://www.youtube.com/watch?v=zV6aueUh8Bs 3. http://www.youtube.com/watch?v=LWoCfTyWG_E