CSC/ECE 517 Fall 2010/ch2 2d dg: Difference between revisions
(Updated Version of Scaffolding in Web Application Frameworks) |
No edit summary |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
__TOC__ | __TOC__ | ||
=Overview= | =Overview= | ||
==Scaffolding in General== | ==Scaffolding in General== | ||
Line 11: | Line 10: | ||
==Scaffolding in Programming== | ==Scaffolding in Programming== | ||
It is with the same basic idea that scaffolding in web programming frameworks refer to. It refers to a temporary structure, which is useful as a starting point for | It is with the same basic idea that scaffolding in web programming frameworks refer to. It refers to a temporary structure, which is useful as a starting point for developing an application. | ||
Scaffolding is nothing but a basic | Scaffolding is nothing but a basic structure that helps a new programmer to get started quickly. It is meta-programming method to quickly generate a functioning web application with the CRUD functionality. Any web application that interacts with database has 4 basic functions generally called CRUD, which it always needs and these are: | ||
* Create | * Create, | ||
* Read, | * Read, | ||
* Update or | * Update or | ||
Line 20: | Line 19: | ||
A scaffold will generate a functioning web application that allows you to carry out these functions. Usually you get a running web application with not more than a few lines of code. | A scaffold will generate a functioning web application that allows you to carry out these functions. Usually you get a running web application with not more than a few lines of code. | ||
It generates for you what is often termed as boilerplate code. In a MVC framework, scaffolding will usually create basic model, views and controllers and also the database objects needed. | It generates for you what is often termed as [http://en.wikipedia.org/wiki/Boilerplate_(text) boilerplate code]. In a [http://en.wikipedia.org/wiki/Model–View–Controller MVC] framework, scaffolding will usually create basic model, views and controllers and also the database objects needed. | ||
==Features of Scaffolding== | ==Features of Scaffolding== | ||
Line 43: | Line 42: | ||
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. | 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 | 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 Origin in Web Applications=== | ||
Scaffolding has been around in various forms for some time. Earlier, separate code generators were written, which would generate CRUD functionality for certain frameworks. They were often the third party generators | Scaffolding has been around in various forms for some time. Earlier, separate code generators were written, which would generate CRUD functionality for certain frameworks. They were often the third party generators which offered varying capabilities and mainly acted as database code generators. | ||
You 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 | You 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. | ||
==What Scaffolding is Today?== | ==What Scaffolding is Today?== | ||
===Current Scaffolding Scope=== | ===Current Scaffolding Scope=== | ||
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 | 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, | *Ruby on Rails, | ||
Line 71: | Line 66: | ||
We will see how this scaffolding works in some of the frameworks described above in next few paragraphs. | We will see how this scaffolding works in some of the frameworks described above in next few paragraphs. | ||
=Scaffolding in MVC based Web Frameworks= | |||
Many web frameworks make use of Model-View-Controller architecture in order to modularize their code. MVC framework separates the business logic, controller and the view part, which increases readability, understandability and reusability. 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== | ==Scaffolding in Rails Framework== | ||
Line 88: | Line 77: | ||
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. | 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. | ||
Rails scaffolding helps to create model, view and controllers for the new resource in a single operation. | Rails scaffolding helps to create model, view and controllers for the new resource in a single operation. | ||
Follow these steps in the [http://www.eclipse.org/ Eclipse IDE] to create a scaffolding of a car website. | Follow these steps in the [http://www.eclipse.org/ 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 -1. Create New Rails Project: New->New Rails Project | ||
Line 96: | Line 85: | ||
Step -3. Make sure current project for Generator is the project that you want. | Step -3. Make sure current project for Generator is the project that you want. | ||
Step -4. Select the scaffold generator from drop down box: | Step -4. Select the scaffold generator from drop down box: Generator->scaffold | ||
Generator->scaffold | |||
Step -5. In parameters, specify the model, fields and their type | Step -5. In parameters, specify the model, fields and their type | ||
Here we take, Car color:string maker:string year:date | |||
Step -6 | Step -6. This will run the generate script and create files in View, controllers, model, db etc as shown below: | ||
<pre> | <pre> | ||
Line 160: | Line 147: | ||
end | end | ||
</pre> | </pre> | ||
Step - | Step -7 You then run a rake task to actually create the database tables using the migrate file. | ||
Step - | Step -8 Now go to Window->Show View->Rake | ||
Step - | Step -9 In the Rake window, select migrate task from the drop down box and run it. | ||
You can also right click the | You can also right click the | ||
Line 173: | Line 160: | ||
rake db:migrate | rake db:migrate | ||
</pre> | </pre> | ||
Step - | 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. | 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. | Sample auto generated code for the above example is as below. | ||
Line 362: | Line 349: | ||
http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki1b_9_ss | http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki1b_9_ss | ||
==Benefits of Scaffolding== | |||
We have noticed that scaffolding makes the developers job easier, and rather than making a lot of configuration changes at the very first time or may be at the time of updations, he can concentrate on the business logic of the application, which saves lot of time that he may have spent in creating and maintaining the database code. So you can read out the benefits of Scaffolding below, | |||
*It acts as a tool for the developers to quickly generate running web application environment for the Internet. | |||
*It provides modular stubs to perform typical CRUD implementations. | |||
*It improves development productivity for system architects. | |||
*It improves efficiency by creating reusable components for developers by using frameworks and code generators. | |||
==See Also== | ==See Also== |
Latest revision as of 04:12, 5 October 2010
Scaffolding in Web Application Frameworks
Overview
Scaffolding in General
Scaffolding in education is basically the support a teacher gives to a new student to get him started on his subject. Scaffolding is also the temporary structure often used by workers in construction. So what we know of the word scaffolding in real world – a temporary structure to lean on while you build the main structure. The temporary structure can then be discarded.
Scaffolding in Programming
It is with the same basic idea that scaffolding in web programming frameworks refer to. It refers to a temporary structure, which is useful as a starting point for developing an application. Scaffolding is nothing but a basic structure that helps a new programmer to get started quickly. It is meta-programming method to quickly generate a functioning web application with the CRUD functionality. Any web application that interacts with database has 4 basic functions generally called CRUD, which it always needs and these are:
- Create,
- Read,
- Update or
- Delete a record.
A scaffold will generate a functioning web application that allows you to carry out these functions. Usually you get a running web application with not more than a few lines of code. It generates for you what is often termed as boilerplate code. In a MVC framework, scaffolding will usually create basic model, views and controllers and also the database objects needed.
Features of Scaffolding
In software applications, we need to use a number of frameworks to make application run. The frameworks may include:
- Database access
- Screen design, and
- Business rules
And scaffolding is the mechanism by which we can create the skeletal of the web application at first go. So, now after having some basic concept of scaffolding, we can list out the main features of scaffolding below,
- It is a framework that provides the minimal setup of the components like database, application servers, and web servers.
- When you want to use the structure early on in the development cycle when you are experimenting with the database schema and layout, it helps to quickly generate basic functional application for you directly from the database schema.
- Scaffolding helps when the schema undergoes refinement and you don't need to waste your time to change the views and controllers explicitly to align with the change.
- It isn't what you would like to use directly in your production systems, you can eventually make changes as you move on with your added and updated project requirements.
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, separate code generators were written, which would generate CRUD functionality for certain frameworks. They were often the third party generators which offered varying capabilities and mainly acted as database code generators.
You 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.
What Scaffolding is Today?
Current Scaffolding Scope
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. We will see how this scaffolding works in some of the frameworks described above in next few paragraphs.
Scaffolding in MVC based Web Frameworks
Many web frameworks make use of Model-View-Controller architecture in order to modularize their code. MVC framework separates the business logic, controller and the view part, which increases readability, understandability and reusability. 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
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. Rails scaffolding helps to create model, view and controllers for the new resource in a single operation. 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
It creates the db migrate file for the given model. This creates the schema in db/migrate/2002301293_create_cars.rb file
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
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
Sample view file for EDIT generated in views->cars->edit.html.erb
<h1>Editing car</h1> <% form_for(@car) do |f| %> <%= f.error_messages %> <p> <%= f.label :color %><br /> <%= f.text_field :color %> </p> <p> <%= f.label :maker %><br /> <%= f.text_field :maker %> </p> <p> <%= f.label :model %><br /> <%= f.text_field :model %> </p> <p> <%= f.label :year %><br /> <%= f.date_select :year %> </p> <p> <%= f.submit 'Update' %> </p> <% end %> <%= link_to 'Show', @car %> | <%= link_to 'Back', cars_path %>
Database migrate file generated in db->migrate
class CreateCars < ActiveRecord::Migration def self.up create_table :cars do |t| t.string :color t.string :maker t.integer :model t.date :year t.timestamps end end def self.down drop_table :cars end end
Database schema generated in db->schema.rb file
ActiveRecord::Schema.define(:version => 20100919144829) do create_table "cars", :force => true do |t| t.string "color" t.string "maker" t.integer "model" t.date "year" t.datetime "created_at" t.datetime "updated_at" end end
Dynamic Scaffolding in CakePHP
Scaffolding in CakePHP is a little more limited than Rails. Unlike Rails, database tables and schema are created separately in CakePHP. It follows the dynamic scaffolding paradigm where you don't need to run an external command to generate the scaffolded code, what is needed is just to define the $scaffold variable and scaffolding is taken care of.
Steps for code generation in CakePHP
Step -1 Create the database schema and the tables.
Step -2 Each table that has to be scaffolded should have a corresponding controller.
Step -3 That controller must define the variable $scaffold For eg, considering our cars example, lets say we have a cars controller-
<?php class CarsController extends AppController { var $scaffold; } ?>
This will go in CarsController.php in app/controllers directory. That is all that is required. The framework will now auto-generate the basic functionality for create, update, delete.
Other Examples
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
Benefits of Scaffolding
We have noticed that scaffolding makes the developers job easier, and rather than making a lot of configuration changes at the very first time or may be at the time of updations, he can concentrate on the business logic of the application, which saves lot of time that he may have spent in creating and maintaining the database code. So you can read out the benefits of Scaffolding below,
- It acts as a tool for the developers to quickly generate running web application environment for the Internet.
- It provides modular stubs to perform typical CRUD implementations.
- It improves development productivity for system architects.
- It improves efficiency by creating reusable components for developers by using frameworks and code generators.
See Also
You can go through some of the other frameworks to create scaffold web applications.
Bibiliography
References
- 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
- Agile Web Development with Rails, 3rd ed. by Dave Thomas, David Heinemeier Hansson, David Hansson, Publisher: Pragmatic Bookshelf
- http://msdn.microsoft.com/en-us/library/cc488469.aspx
- http://guides.rubyonrails.org/getting_started.html
External Sites
- http://en.wikipedia.org/wiki/Scaffolding_Theory
- http://www.wisegeek.com/what-is-web-application-scaffolding.htm
- http://www.developer.com/lang/php/article.php/3636686/Scaffolding-with-CakePHP---Managing-Your-Fantasy-Football-Team.htm
- http://cpan.uwinnipeg.ca/htdocs/Scaffold/Scaffold.html