Saturday, October 2, 2010

How do you write a functional CRUD application in 3 minutes?

I came across a wonderful framework recently and was totally blown away by it. The simplicity of the framework, totally boast productivity to a new level.

Grails, an open source web application framework, aims to reduce the traditional XML configuration of Enterprise Java Application Development to the minimum. It favors convention over configuration, and to a programmer like me who hates to see another line of XML, Grails is like a God sent. Grails has been designed based on Spring and Hibernate framework, and it can be used to build Enterprise Grade Application.

The following is a simple design principle taken from Wikipedia:

"XML was initially welcomed as it provided greater consistency to configure applications. In recent years however it has become apparent that although XML is great for configuration it can be tedious to set up an environment. This may take away productivity as developers spend time understanding and maintaining framework configuration as the application grows. Adding or changing functionality in applications which use XML configuration adds an extra step to the change process next to writing application code which slows down productivity and may take away the agility of the entire process.

Grails takes away the need to add configuration in XML files. Instead the framework uses a set of rules or conventions while inspecting the code of Grails-based applications. For example, a class name which ends with Controller (for example BookController) is considered a web controller."

Anyway, you can read up the background and theory later if that interest you. My main goal today is to show how easy it is to build a simple Grails application within 3 minutes.

After downloading the latest Grails from their download pagehere, you can proceed with the few simple steps to it up rather quickly.

OK, lets review our objective today:

  • I want to build a simple Application to track what sort of cars my customers drive

  • To achieve that, I will need a simple customer object - with the usual first name, last name, birthday, income group, and the kind of car he drives.

  • As I want to control the sort of cars I have in my database, I will just create a car object. In which you can find all sort of cars in there.

  • I will also create an income group object. That way this parameter can be configurable as well.


  • So all set. Lets start.

    First thing I do is to create a grails application. This can be done by simply issuing the following command. Lets call our application CIS [Customer Information Portal]:
    grails create-app CIS

    You will see a tons of texts flying out from your screen. That means grails is generating the skeleton of the project for you. At the end of it, you will see the following line and then you know your application has been successfully created:
    Created Grails Application at /Users/lys/Test/CIS

    Now, we need to create three domain class. A domain class in Grails is basically a normal Groovy class. We will get into this in another article. From the above objective, we know we need to create three domain classes, namely:
    1. Customer
    2. Car
    3. Income Group

    So lets get to work. We can simply create the domain class by issuing the simple commands:
    1. cd CIS
    2. grails create-domain-class Customer
    3. grails create-domain-class Car
    4. grails create-domain-class IncomeGroup


    If we now navigate to CIS/grails-app/domain/cis, we can see that three files have been generated by Grails:
    1. Customer.groovy
    2. Car.groovy
    3. IncomeGroup.groovy

    Lets go ahead and define our customer object. Say it has the following properties:
    1. First Name
    2. Last Name
    3. Birthday
    4. Car
    5. Income Group

    This is very simple, eh? So lets go ahead and define it in the customer.groovy file:
    package cis

    class Customer {

    String firstName
    String lastName
    Date birthday
    Car vehicleMake
    IncomeGroup annualIncome

    }

    And we will go ahead to define the other two objects. To make things really simple, I am putting income group as a string. You can always make it more complicated to meet your needs:
    package cis

    class Car {

    String manufacturer
    String model
    Integer displacement

    }

    package cis

    class IncomeGroup {

    String annualIncome

    }

    OK. We are done now. And that barely took 1 minutes. Now lets grails do the dirty work. We need to build those objects now. So issue the following commands:
    1. grails generate-all cis.Customer
    2. grails generate-all cis.Car
    3. grails generate-all cis.IncomeGroup


    We are all done by now, and YES, our application is ready to run! Issue this command and see what you got:

    grails run-app cis

    Grails at this point will start up the web server, and your application is served. You can view your application here:
    http://localhost:8080/CIS/



    Great! All our controllers are there. Click at cis.CustomerController, and you will see a simple list where all the customer are supposed to be. Click at create a new customer, and the following screen came up.

    I have taken the liberty to fill up some of the information I have. Since we have not defined any car make and income group yet, I am going to leave it empty.



    Click "Create". Oh No! I cannot create! Grails is telling me that the car make and income group fields are mandatory.



    But wait! I did not even do a single line of coding. All I did was putting in all the properties for each object only.

    Yes, in that 3 minutes we did this, Grails has generated the web front end, the domain class, the database tables, and the Validation logic for you!

    Ok, now lets put in all the correct properties for Income Group and Car and we can get this over with. The following is the final result. All the data are tabulated in a list. The view can be easily changed if we need to; but this is what we have for now:



    Click at the Id, and it brings me to the detail page:



    Click at the Income group, and it brings me to the income group page.



    Not bad for 3 minutes work huh?

    1 comment:

    1. Syntax:
      grails application development

      Nice Blog,From This I came To Know About How do you write a functional CRUD application in 3 minutes with examples.

      ReplyDelete