Friday, July 23, 2010

Groovy on Grails

I've been playing with Groovy and Grails for the past month. We had a demand of a few sites that needed to be constructed. The sites were small and needed to have some basic CRUD method and a small business rules. Then, we had to tie it with our SMS Gateway that we have developed. We evaluated a few languages, but at the end we chose Groovy for the following reasons:
  1. Dynamic language
  2. It sits in the JVM
  3. Easy to develop sites
We have been playing with the following versions:
  • Grails 1.3.3
  • Groovy 1.6.0_20
I have the most respect for the team who build this programming language. The language is very easy to read and the learning curve is very small. The integration that they did with Java, Spring, and Hibernate is very nice. The ORM tool that they created, GORM is very nice and very easy and quiet frankly it's my favorite.

class User {

String userId
String password
String homepage
Date dateCreated
Profile profile


User has a one to one relation with profile.

class Profile {
static belongsTo = User

byte[] photo
String fullName
String bio
String homepage
String email
String timezone
String country
String jabberAddress

static constraints = {
fullName(nullable: true)
bio(nullable: true, maxSize: 1000)
homepage(url: true, nullable: true)
email(email: true, nullable: true)
photo(nullable: true)
country(nullable: true)
timezone(nullable: true)
jabberAddress(email: true, nullable: true)


String toString() {
"Profile for ${fullName} (${id})"

The belongsTo is assigned the owning class, which means the relationship is unidirectional. You can get to a Profile via a User, but there’s no link back from a Profile to a User

class Post {
String content
Date dateCreated

static constraints = {
content(blank: false)
static belongsTo = [user: User]

static mapping = {
profile lazy: false
sort dateCreated: "desc"

static hasMany = [tags: Tag]

You can sort automatically within the mapping closure. In this example, all queries to the Post object will return in a descending order.

We use the map style of belongsTo, where we create a bidirectional link between User and Post classes. This creates a new field on Post called user that is the bidirectional mapping back to the owning User

Another useful technique is the validation of the fields. In the constraint closure you can specify the fields that need to be validated and the validation type. For example, in this case, content cannot be blank (null or empty).

The first deployment that I made was using "grails war". The con of this approach is that I look the dynamic compilation. So, if any change to the code, you would have to create the war once again, and deploy it. You gain performance but you loose the dynamism of Groovy. If performance is not a big issue, you can use "grails run-war". According to the documentation:
This is very similar to the previous option, but Tomcat runs against the packaged WAR file rather than the development sources. Hot-reloading is disabled, so you get good performance without the hassle of having to deploy the WAR file elsewherthe application is compiled with Java native code. Therefore, every change that I have to make I have to do it in Grails and deploy it once again.

I welcome your feedback in case I'm wrong.

No comments:

Post a Comment