CSC/ECE 517 Fall 2009/wiki1b 9 ad

From Expertiza_Wiki
Revision as of 19:48, 27 September 2009 by Artonoops (talk | contribs)
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 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.


How Dynamic Scaffolding was done in Rails

Run the script/generate command on a model and then use the migrate file, which is created to layout columns in the model's database table. Next run the script/generate command on a controller and then add scaffolding.

Code

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

The latest 'Scaffold code generator'

In Rails 2.0 it takes fewer steps than shown above, but may be a little harder to follow because so much is accomplished with so few commands. ruby script/generate scaffold Model_Name [parameters] command accomplishes the task of generating the model, plus scaffolding, and the database migration script needed as well as a controller, helper, and testing support files.

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.


Scaffolding in frameworks for other languages

Many other languages such as Java, .NET, 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. Unlike Rails 2 CakePHP supports dynamic scaffolding. Scaffolding in CakePHP is achieved by setting the $scaffold variable in the controller. The scaffolding feature 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.

When compared with Rails, Rails is slightly faster because of its scaffolding feature which generates some html as well as the model-interaction code, which can be tweaked to ones own liking. The one downside is Rails does not provide its own user authentication system, but there are plenty of plugins that implement that. Customization of the generic views in Django is possible but needs to be done manually.

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 in no time.

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. Importantly, the framework lets you design your classes that generate your database, rather than designing your database to create your classes. 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.

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 ORM of choice to access data. It provides numerous ways with a few to name like MSFT, ADO .NET - the MS Entity framework etc..

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.

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. Code generation in Seam-gen requires few configurations as against the Rails' "script/generate" single line command, which simply works.

Grails for Groovy

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

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