Wednesday, November 12, 2008

Using Grails to explore and develop the domain model

Data modeling is typically a fairly important design activity. However, I have a hard time with EAR diagrams and data models as a starting point – particularly for a new system (you don’t really have a choice when dealing with legacy databases). Thinking in objects is much more natural to me. Also, I like the idea of using code to explore the domain model and try a few things out.

In the past, I have used the forward engineering features of Hibernate for this very purpose. Once the tables have been created in the database, I take advantage of a neat feature in JDeveloper where you can reverse engineer a data model diagram. So the process kind of goes like this Object model->Java code->Hibernate DDL->Database->Data model. It worked well in thrashing out the data model for my last project. Of course, it may not work for everyone but that’s ok – it works for me and that’s all that matters ;-)

For a new project I’m working on, I thought I’d try something different. I thought I’d give Grails a spin. This worked even better because Grails can generate such a lot of boiler-plate code for me enabling me to move a lot faster. It can also generate a scaffolding UI so I can interact with and test out the model. Here was the process:
  1. Download and install Grails from grails.org

  2. Follow the Quick Start guide to familiarize yourself with Grails http://grails.org/Quick+Start . During the Quick Start guide, you will learn how to create an application a domain class and a controller

  3. Configure Grails to use Oracle instead of HSQL (I needed to externalize the database so I could browse it and reverse engineer the data model diagram using JDeveloper) – to do this modify grails-app/conf/DataSource.groovy – change the driver class name and the JDBC url – you may also have to copy the Oracle JDBC driver (ojdbc14.jar) to the lib directory

  4. For each entity (where name is the name of the entity you want to model)

    1. $ grails create-domain-class

    2. Modify the generated class located in grails-app/domain/name.groovy – add the attributes

    3. $ grails create-controller

    4. Modify the generated controller class located in grails-app/controllers/nameController.groovy – change the body of the class to look like def scaffold = name

  5. Run the application $ grails run-app

  6. Point your browser to the application

  7. Populate the model by interacting with the controllers through the generated scaffolding

  8. Add some more entities by going to back to step 4

  9. If you want to auto-populate the model with data on startup, add your code to grails-app/conf/BootStrap.groovy

I have to say, I found the out of the box experience with Grails pretty polished. It passed the 15 minute test with flying colors. (If I can’t get something working in under 15 minutes, I tend to dump it).

1 comments:

joeyGibson said...

I haven't done too much with Grails yet, but I am seriously digging it. I love Rails, but having the scalability of a Java web container, plus Spring and Hibernate, makes a very compelling argument for Grails.