CSC/ECE 517 Fall 2009/wiki1b 9 ss

From Expertiza_Wiki
Jump to navigation Jump to search

Scaffolding in Web application frameworks

Topic :We have discussed scaffolds for Ruby on Rails. The dynamic scaffold of Rails 1.x was replaced in Rails 2 by a generator of scaffolds for specific tables. Why did this change take place? What are the pros and cons of dynamic scaffolds? Many languages other than Ruby (Java, Python, and PHP, for example) provide Web application frameworks. Do any of these languages have the equivalent of scaffolding? If so, how does it compare to Rails?

The concept of Scaffolding

Scaffolding in web frameworks allow a software developer to create data-driven web applications quickly by automating CRUD (Create, Read, Update, Delete) data operations. It achieves this by generating pages for the models within the MVC architecture of the application that have a working data form that can communicate with the database. This allows the developer to get an early look at the data presented in the application as a raw design from which they can build, modify, or rework the application in order to achieve a final product.

For the young developer, scaffolding has the advantage of quickly and easily building a working database driven application. It also shows the developer what kinds of methods and logic must be present within the applications controllers in order to produce a successful product. Scaffolding is a pragmatic tool to help young developers learn about creating web applications.

For the experienced developer, scaffolding gives them fast access to viewing and editing pages. With this the experienced developer can then customize the application through the use of metadata, templates, overriding the scaffold actions with custom ones, or they may use the quickly generated build as a rough draft from which they will write their own structure.

Scaffolding has become very popular within in the web development community and therefore is showing up in several web frameworks. This is mostly because it increases work flow and it allows web designers to spend more time designing instead of building interfaces to data. Scaffolding has become so wide spread that it can be found in such frameworks as Ruby on Rails, Python, PHP, Java, and .NET.

Dynamic Scaffolding

Dynamic Scaffolding generates the data interfaces at run time. With very little code written and no necessary knowledge of the underpinning data interfaces a developer could get a web application up and running in very little time and with little effort with Dynamic Scaffolding. Young developers loved it because they could get a web application up and running without having to know the gory details, or even much about the framework they were working with. Many experienced developers also like it because it allowed them to quickly prototype an application and wow clients as they see their product come to life in a matter of minutes. There were problems with Dynamic Scaffolding however. Because Dynamic Scaffolding creates the data interfaces at run time, these interfaces are hidden from the developer and to them, it's rather magical in its implementation. This creates some problems. One problem is the developer does not actually learn how to handle a typical CRUD scenario. Even if the developer did know how to build and customize their own CRUD scenario, the code that is created is hidden which makes it very difficult to access and work with. Because of these problems, many application development purists see Dynamic Scaffolding as doing more harm than good.

Scaffold Generation

Scaffold Generation is another approach to scaffolding that generates the data interfaces for a developer, but also gives them access to the generated code. This process is a bit less automated than Dynamic Scaffolding because changes to the model will not be automatically updated elsewhere in the application. What the developer loses in automation, they gain in having a working site filled with examples from which to learn from. They also have immediate access to the data interfaces so they can make modifications quickly and efficiently. Scaffold Generation also has the advantage of reducing the learning curve from a scaffold created controller to a custom one. With Dynamic Scaffolding is was easy for a developer to create a simple CRUD interface, but there was a big gap between that step and a custom interface that handles a specific job with specific needs. This gap is not nearly as wide now that the developer has access to the code created through Scaffold Generation and can use it as a foundation to branch out into custom interpretations of the simple CRUD scenario.

Scaffolding in Ruby on Rails

Scaffolding became popular in Ruby on Rails and has since moved on to several other frameworks. The first generation of Ruby on Rails supported Dynamic Scaffolding. It also supported Scaffold Generation however books, demos, and tutorials based on the first generation of Ruby on Rails largely ignored this fact. Beginning with the second generation of Ruby on Rails Dynamic Scaffolding was no longer supported and Scaffolding Generation took center stage.

Ruby on Rails 1.x

In the first generation of Ruby on Rails a developer would generate a modal, then use a migrate file to create layout columns in the model's database table, and after that they would generate a controller and add scaffolding. A scaffolding example in Ruby on Rails 1.x would look like...

scaffold :modelname

Ruby on Rails 2.x

Ruby on Rails 2.x may have taken away Dynamic Scaffolding but it made Scaffold Generation simpler and involving less steps. This is due to being able to create your model, migrate file, and column layout all in one step. A scaffolding example in Ruby on Rails 2.x would look like...

./script/generate scaffold ModelName field1:type field2:type field3:type

Once this script is run the generator will build 14 files in your application, along with some folders, and edit one more. Here’s a quick overview of what it creates:

  • The model with the name you specified as ModelName
  • Migration script to create the ModelNames table in your database
  • A set of views for add, edit, display, and delete
  • A view to control the overall look and feel of the views
  • A cascading style sheet to make the scaffold views look better
  • the ModalName controller
  • Testing harness for the ModalName controller
  • Helper functions
  • Routing information
  • Unit testing harness for ModalName
  • Unit testing harness for ModalName helper

Scaffolding in Python

Django

Django is a high-level python web framework that was written in a newsroom to automate as much as possible for web developers under heavy deadlines. In Django you find scaffolding in what it calls the admin interface. The idea behind the admin interface is that user pages differ greatly from client to client, but that admin tools largely remain the same and can be very repetitive to develop over and over, therefore it makes since to create a tool that will create these types of applications for us. By hooking to the metadata in your model, a full functional web application can immediately be used as an administration tool to help manage your site. The way the admin interface is implemented is for each model that you want to be associated with the admin interface you mark that model. Then in your url.py file you add the url for the admin interface. This would look something like...

 urlpatterns = patterns('',
    (r'^admin/', include('django.contrib.admin.urls')),
 )

This use of scaffolding in Django quickly and easily created an entire administration application.

Scaffolding in PHP

CakePHP

When Ruby on Rails began to take off many frameworks decided to model themselves after what Ruby on Rails was doing. CakePHP is a web development framework written in PHP that has incorporated many of the same concepts and design principles as Ruby on Rails. Because of this CakePHP prides itself on supporting scaffolding.

CakePHP really has two types of scaffolding, one that it is actually called Scaffolding and another that is called Bake. What CakePHP calls scaffolding is really like Dynamic Scaffolding. Here you have a modal and then in your controller you declare a scaffold variable and CakePHP handles the rest behind the scenes. An example would look like this...

<?php

class MyController extends AppController {
    var $scaffold;
}

?>

The Bake console with CakePHP geneerates code for the models, views, and controllers. Bake works like Scaffold Generation and creates a fully functional structure a developer can learn from or build upon. An example would like like this...

cake bake model [model name]
cake bake controller [controller name]
cake bake view [view name]

Scaffolding in Java

GRails

GRails is an open source framework that integrates with a programming environment called Groovy which is based on the Java platform. GRails supports Dynamic scaffolding however it does allow a developer to access the generated code if they want it. If a developer tells GRails to install templates then the current scaffold code will be created in a template file that can be modified to fit specific needs. GRails supports other Java technologies such as Spring and Hibernate. An example snippet of scaffoldng in GRails would look like...

class myController{
 def scaffold=true
}

Appfuse

Appfuse is an open source project and application that uses open source tools built on the java platform to help a developer build web applications quickly. Appfuse is not actually a framework. Appfuse is more of a tool for code generation that can be used with other Java web frameworks. It can be integrated into other open source technolgies such as Spring, Hibernate, and Struts.

Scaffolding in .NET

Additional Implementations

Flash

Javacript

GRails