CSC/ECE 517 Fall 2009/wiki1b 9 ad

From Expertiza_Wiki
Jump to navigation Jump to search

Scaffold

Scaffolding is a meta-programming method of building database-backed software applications. It is a technique that allows a programmer to quickly generate a skeleton user interface for an application. This interface then allows the user to perform basic CRUD ( Create Read Update Delete) operations on the application's database. The auto-generated interface can then be modified to perform more powerful tasks.

The concept of scaffolding was made popular by the Rails framework with its dynamic scaffolding. Dynamic Scaffolding enabled programmers to develop quick and simple interfaces on the go during the initial stages of application development, when the database schema is still shaping up.

Dynamic Scaffolding

Dynamic Scaffolding is a method of generating scaffolds at run time or on the fly. Dynamic scaffolding can be achieved by placing the keyword scaffold with the model name in the controller class as shown below.

class ContactController < ApplicationController
scaffold contact     # contact here is the name of the model called contact
end

When this code is executed Rails generates all the appropriate data interfaces. Dynamic scaffolding was removed in Rails 2 and replaced by a generator of scaffolds.

Why was dynamic scaffolding removed in Rails 2?

Dynamic Scaffolding of Rails 1.x was replaced by a generator of scaffolds in Rails 2. David Heinemeier Hansson responding to a new rails developer on a Ruby forum said,

"Dynamic Scaffolding didn't really help people learn about Rails or give them a way to modify the generated interface, so we killed it.".

In dynamic scaffolding when the line scaffold :model_name is added to the controller rails automatically generates the appropriate interfaces at run time. Since, scaffolding is implemented on the fly the programmer cannot easily modify and customize the interfaces.Therefore, dynamic scaffolding was removed in Rails 2 though it is still available as a plugin.

Pros and cons of Dynamic Scaffolding

Pros

  1. The biggest advantage of Dynamic scaffolding is that it allows the programmer to extend and modify the database schema without worrying about extra coding to reflect those changes in the interface. The user interface automatically updates to keep up with the changes.
  2. The code to generate scaffolds dynamically, is simple in most frameworks and a lot is accomplished in a single line of code.

Cons

  1. The biggest disadvantage of Dynamic scaffolding is that the programmer cannot see whats going on. Since all the interfaces are generated on the fly, the code is hidden; this could be especially disadvantageous to someone who is trying to learn Rails.
  2. The dynamic nature of generating scaffolds makes the behaviour of the application a little unpredictable.
  3. It is difficult to modify or customize the scaffold code since it is generated at run time.

The generator of Scaffolds

The generator of scaffold takes the name of the model ie the resource and optionally a list of field names and types as shown below in the command line and automatically adds the appropriate data interfaces to the application.

cmd> ruby script/generate scaffold modelname \ field1:type field2:type ....

Unlike dynamic scaffolding the scaffold generator generates the scaffolds explicitly. One drawback to this is that the model cannot be updated automatically when a new field is added to the table. The programmer will have to run the script generate/scaffold command or manually update the model.

Scaffolding in frameworks for other languages

Many other languages such as Java, .NET languages, PHP, Python have frameworks that have adapted scaffolding. Let us walk through some of the frameworks that provide the scaffolding feature such as CakePHP, Codelgniter, Symphony and Yii for PHP, Django for Python, MonoRail and ASP.net for .Net, Seam for Java and Grails for Groovy.

CakePHP for PHP

CakePHP is a rapid development environment for PHP and uses commonly known design patterns like MVC and ORM. It is modeled after the concepts of Ruby on Rails and hence has many similarities with it. Scaffolding in CakePHP is implemented by adding the $scaffold variable in the controller:

<?
php class ContactController extends AppController 
{     
   var $scaffold; 
} 
?> 
Comparing RoR with CakePHP
  • Unlike Rails 2 CakePHP supports dynamic scaffolding.
  • Just like RoR the scaffolding feature in CakePHP analyzes the database tables and creates standard lists with add, delete and edit buttons, standard forms for editing and standard views for inspecting a single item in the database.


Django for Python

Django is an open source web application framework, written in Python, which follows the model-view-controller architectural pattern. Just like Rails, Django aims at providing the concept of 'DRY' and extensive reusability. With regard to scaffolding, once the models are defined, Django can automatically create a professional and production ready administrative interface -- a Web site that lets authenticated users add, change and delete objects. The framework offers role-based authentication and also one does not have to deal with creating backend interfaces just to manage content. It uses generic views to help automate CRUD operations that rails scaffold provides, minus the html generation.

Comparing RoR with Django
  • Customization of the generic views in Django is not done automatically.
  • Rails does not provide its own user authentication system, however, there are custom plugins that implement this feature.


MonoRail for .net

MonoRail, a component of the Castle Project, is an open source web application framework built on top of the ASP.NET platform. Like scaffolding in Ruby on Rails (RoR), controllers adorned with the necessary scaffolding attributes can automatically produce the basic markup and logic for CRUD operations on the domain model. It proves handy for prototyping or creating rough-and-ready data entry capabilities in your applications.

Comparing RoR with MonoRail
  • MonoRail gives the flexibility to chalk out responsibilities that should be handled by Scaffolding. i.e., Its implementation allows you to override the views if you want, and the scaffolding will handle only the CRUD operations.
  • Both MonoRail and RoR let you design your classes that generate your database, rather than generating your classes from a given database schema.
  • MonoRail, just like Rails, maintains a standard ORM for data access throughout its applications.


ASP.net Dynamic Data

ASP.NET Dynamic Data Support, a part of the ASP.NET 3.5 Extensions project provides a rich scaffolding framework that allows rapid data driven site development using both ASP.NET WebForms and ASP.NET MVC. By default ASP.NET Dynamic Data projects provide built-in support for creating automatic "scaffolding" views of LINQ to SQL and LINQ to Entities data models. Dynamic Data projects include template pages and user controls that can do CRUD operations are dynamically constructed at runtime based on the LINQ to SQL or LINQ to Entities data model that has been added to the project.

Comparing RoR with ASP.net Dynamic Data
  • Unlike Rails' dynamic scaffolding, customization of views is possible.
  • It includes a .NET Attribute approach, which might be used to add formatting or range validation to the data model directly.
  • As against Rails, Dynamic Data has no specific choice of ORM to access data. Any ORM like ADO .NET - the Entity framework, NHibernate etc. could be used.


Seam for Java

Seam is a powerful open source development platform for building rich Internet applications in Java. It provides a scaffolding code generation tool called seam-gen. Seam-gen allows the developer to begin developing a custom application without having to spend time piecing together the build, configuration, and dependencies. It makes use of Hibernate ORM tools and Freemarker templates to generate the front-end JSF code as well as Java classes required by the application.

Comparing RoR with Seam
  • Code generation in Seam-gen requires a couple of configuration steps whereas Rails accomplishes the same with the'single line command ("script/generate").
  • The java classes implement Home design pattern which is much cleaner than the ActiveRecord design pattern used in Rails as it separates out the object and operations.


Grails for Groovy

Grails, is a push MVC web framework just like Rails but it uses the Groovy language.

Comparing RoR with Grails
  • Grails offers the Implicit Dynamic Scaffolding (optional) feature, as against Rails, which does not offer it anymore. This can be useful when learning the framework.
  • The view (html) is generated, and adapts automatically to the changes in the model like Rails.
  • Both frameworks also have explicit scaffolding.
  • The scaffold generated by Rails is more elegant, but Grails has the extra feature of column sorting.


Resources

Abbreviations