Thursday, July 5, 2007

My Grails Practices

After learning Grails and finish 2 small project with it in 4 months time, here are a number of the practices I do to develop faster and comfortable for me. As I am new to Grails as well as to java web development, the practices here may not be wrong and your comments are very much welcome.

A controller's action performing post and get.

I have a lot of that in my project, where it really helps me organize my code to be more readable and maintainable and less confusing. This is not against to be CRUD pattern that grails generate, but somehow I feel not comfortable with it, because I have to think two action's name instead of one. For example, I have view employee profile page where you can extends the the employee's contract or terminate the employee's contract. Here's how I code it

def extendEmployeeContract {
if (request.method == 'GET') {
// preparing the page for extending the employee's contract
}
else if (request.method == 'POST') {
// performing the employee's contract extension
}
}

instead of

def createEmployeeContractExtension {
// preparing the page for extending the employee's contract
}

def saveEmployeeContractEntersion {
// performing the employee's contract extension
}


Restricting an action to post only if applicable

Here's how grails make it easy for you, just define it in the allowedMethods attributes.

def allowedMethods = [checkEmployee:'POST']

This makes your application much more secure.

Do you manual assignment after object.properties = params

Sometimes I need to do some manual assignment when an attribute is your domain class, and be able to validate the object that we want to assign to an object's attribute. Doing so we avoid a hibernate error saying that a object's identifier is alter from 1 to 2 which are the id of the object.

Use java.sql.date for date attribute

Using java.sql.date could result into a date field on your database while java.util.date could give you a datetime field in your database which give me a problem when I perform a query with the dynamic find method. This gives me additional work to do because the object.properties = params won't work here but this bug is fix and will be on the next release.

Another reason I use java.sql.date is because of how I implement my user interface. I am not using the gsp datepicker tag, instead I use a regular text field which is much faster to input in for the user and you can use any javascript datepicker solution. For the date format I use yyyy-M-d and it suit very well to java.sql.date, use the java.sql.date.valueOf() static method. Here's how I implement it.

employee.birthday = params.birthday =~ /\d{4}-\d{1,2}-\d{1,2}/ ? java.sql.date.valueOf(params.birthday):null

This validates the user's input and if it's a invalid format, a null will assign to the date attribute which trigger the null constraints I define.

More will be from you, I be glad to hear it.

2 comments:

Anonymous said...

Hello, I enjoy reading through your article. I like to write a little comment to support you.
My page - "how to write" lewis philips

Anonymous said...

What's up, everything is going nicely here and ofcourse every one is sharing information, that's genuinely
good, keep up writing.
Feel free to surf my page - fantasy scifi