Tuesday, August 31, 2010

Grails partial binding

Ever have a domain class that only needs some of the properties set in a controller, and you want to make sure that specific parameters never get set? Here is a quick example of how you can do that.

Let's say we have the following domain class

package com.xyzcorp

class Widget {
Date dateCreated
String sku
int quantity = 0

static mapping = {
autoTimestamp true
}
}

Now imagine we have a controller where we are creating the widget and the dateCreated or quantity properties should never be set by the UI (since our system is maintaining those internally). Here is how:

// some code in our controller
Widget widget = new Widget()
bindData(widget, params, [
exclude: [
'dateCreated',
'quantity'
]
])

As you can see we are using bindData to bind the params presented to our controller to our widget instance while excluding the dateCreated and quantity properties.

You could argue that we should just set the sku property in the constructor, but on larger domain classes it can be easier to exclude properties instead. Plus, our intent is pretty clear for future developers who are maintaining our code that we don't ever want dateCreated or quantity to be set from the UI. A big plus if you ask me!

This is just an example of excluding data from binding and bindData has quite a few other options besides exclude, so I encourage you to explore them.

Groovy and Grails code snippets

Sometimes the best way to learn new things is to read other peoples code. I personally learned quite a lot reading the Spring framework and Spring security source code over the years, but that's not for the faint of heart.

Snipplr gives you the opportunity to browse smaller code snippets and maybe even pick up a few new tricks. Searching around I have found some gems in the Groovy and Grails categories.

Here are a few I found this morning:
  1. Download files from FTP in Groovy using Ant task
  2. Groovy way to create File and URL MD5 hashes

What cool snippets did you find? Let's hear about them!

Monday, August 30, 2010

Code quality

Creating maintainable Grails applications isn't too tough. The convention over configuration approach of Grails helps address a lot of the problems suffered by larger applications in the maintenance phase. It's easy to find where bug fixes, changes, or enhancements should be applied.

However, this doesn't mean there is room for improvement. It's not a fundamental issue with Grails but applications in general. Having a proper set of test suites which cover the vast majority of the applications functionality goes a long way. That's why I have found the coverage plug-in so useful.

The coverage plug-in can help you identify areas within your application where your test coverage is lacking. This might even lead you to find out that the code is difficult to test and possibly designed incorrectly. I have found that if something is hard to test, then it's probably done wrong.

Along with the coverage plug-in, the codenarc plug-in can help you identify issues (some very minor) that will help you build more maintainable Grails applications.

So, take them for a spin and improve your Grails applications!

Saturday, August 28, 2010

Grails testing tips

Don't get me wrong, Grails is fantastic, but the documentation sometimes is lacking or completely wrong. I realize that development is moving so fast with Grails that documentation gets overlooked but this can be a source of frustration.

Often my frustration comes when I am trying to write effective tests and the documentation often send me (and my co-workers) down the wrong path.

In an effort to keep others from making the same mistakes I have made in the past, you may want to just check out the MockUtils implementation to see what Grails really has to offer during testing.

If you aren't familiar with testing in Grails then check out the sample chapter (which happens to be all about testing) from the book Grails in Action. If you don't already have a copy of the book, I recommend you get one. It's fantastic!

P.S. As a bonus here are some nice examples of testing REST controllers.

Got Groovy?

One of the things I really love about the Groovy language is how quickly you can accomplish things. There has been a lot of thought put into making the language easier to use and it really shows.

All though I highly recommend reading through the GDK to really understand all the great stuff that Groovy has to offer, sometimes you just need to know how to do something quickly.

I stumbled upon a nicely organized Groovy version of the PLEAC (programming language examples alike cookbook). It's chalk full of examples on how to quickly accomplish common tasks and is a great demonstration of the power of Groovy.

Grails custom constraints

Geoff Lane over at Zorched.net has a great post about creating your own custom constraints for validation in Grails. While I didn't take the same exact route as he outlined (I didn't create a plug-in) it was amazing how quickly and easily you can add custom constraints to Grails.

I also happened to notice that AbstractConstraint allows for some additional options, such as vetoing which could come in handy if you want to take a "fail fast" approach to constraint violations.

So head over there, and read up!