Monday, March 2, 2009

Top 10 Tips for Grails Developer: Redux (extra bonus tips)

Ok so in my last post about Grails Top 10 Tips i realised I did not really give that many actual grails specific tips :)

So i had long think about the way I use grails and I think I have some more tips. These tips are for when you are doing serious development and not just some throwaway prototype.

1) Follow domain driven development.

This is very very easy to do in grails and is practically common sense to do so (ha ha although in my experience there is no such thing as common sense). So what i mean by this is to follow test driven development starting with your domain first (tddd i guess). Create your domain objects first using the grails macros "grails create-domain". This will also then generate your unit tests for you (how easy is that). Then start fleshing out the unit tests. To make life easy use this plugin (installed default in grails 1.1)

Testing plugin

Now if you can delay gratification and fully develop your domain model first just using unit tests then this will pay off later. You will see as i explain below.

However if you can't wait and you need to see the app come to life then create a controller using "grails create-controller" and use the lovely grails scaffold to have your crud pages generated for you.

Section 16 of the grails docs

However once you are happy with the domain from the view point of the webpages. Then delete the new scaffold controller because the real deal is about to be generated below.

So by now in some shape or form you should have developed your domain model. Or more specifically the parts that you need right now to meet the stories (features) you are building (sorry i have my agile hat on here).

So now you can pull one of the grails gems out of the bag "grails generate-all" or one step better you can install the xtemplates plugin

xtemplate plugin

Then using this plugin you can use "grails uber-xgenerate" and this will create controllers, views and unit tests for all CRUD operations of your new domain model.

This now basically gives you a working app. All you have to do is skin it how you want.

2) Change your default development datasource settings

Once you are building a serious grails app you are going to want to change from the default hsql in- memory database. I actually prefer to use mysql as my deployment db as i can query it easily from the commandline.

Edit datasource.groovy to something like this:

dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "testuser"
password = "testuser"
hibernate {

// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:mysql://localhost/testdb"
dialect = "org.hibernate.dialect.MySQL5Dialect"

test {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:mem:testDb"
production {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:file:prodDb;shutdown=true"

3) Use bootstrap.groovy
This is a very important file. It allows you to populate your database with with your domain model. Here you can create test data and save it to the database.

4) Use command objects
I find these little guys are very useful in some circumstances. Sometimes you want to capture user data using a form that does not exactly map to the way you want to store you info in your domain object or perhaps the information does not even get saved. Examples would be an advanced search form or a file upload form. The great thing about Command objects is they still allow you to specify validation constraints. Here is an example:

class DemoFormCommand {

String email;
String name;

static constraints = {
name(blank: false)
email(blank: false, email: true)

Then in my controller i can add an action like this:

def upload = {DemoFormCommand demoFormCommand ->

if (demoFormCommand.hasErrors()) {



Anonymous said...

Will xtemplate plugin work with grails 1.1 release?

ooblogger said...


Please add your site at is a place where other people can find you among the best sites on the internet!
Its just started and we are collecting the best found on the net! We will be delighted to have you in the sweebs listings.


custom research paper writing services said...

I think Grails is an open source web application framework which uses the Groovy programming language (which is in turn based on the Java platform).

writing academic essays said...

that's right, it is. strange that you doubt about it, man - you'r first thought was right