Grails Best Practices

I’ve worked with grails for about as long or maybe even longer than the author and the list looks good.

Grails Best Practices Posted by Amit Jain on Apr 03, 2012 | 3 comments Share|
I work at IntelliGrape, a company which specializes in Groovy & Grails development. This article is a basic list of best practices that our Grails projects follow, gathered from mailing lists, Stack Overflow, blogs, podcasts and internal discussions at IntelliGrape. They are categorized under controller, service, domain, views, taglibs, testing and general.
The advice here is specifically for Grails 2.0, although much of it is generally applicable.
Controller
Don’t allow the controller to take over another role. The role of a controller is to accept incoming requests, check permissions etc, ask a domain or a service for a result, give the result back to the requester in the desired format such as HTML, JSON, or XML. Keep the controller as thin as possible. Don’t perform business logic, queries, or updates within controllers.
If a controller represents a single domain class, use the standard naming convention of “<DomainClass>Controller”.
Avoid code duplication – common operations should be extracted as a closure or a method. See this blog entry for more information.
Split complex data binding into a command object. You can make command objects rich (just like rich domain classes). Creating a hierarchy of command objects can also be useful in some scenarios.
Service
A service is the right candidate for complex business logic or coarse grained code. If required, the service API can easily be exposed as a RESTful/SOAP web service.
Services are transactional by default, but can be made non-transactional if none of their methods update the persistence store.
Views
Keep views as simple as possible – avoid the temptation to put business or database logic in this layer.
Use layouts to ensure a consistent look across all, or a sub-set of, the application’s pages.
Keep your views DRY (“Don’t Repeat Yourself”). Split the repeated content into templates.
Use custom TagLibs for common UI elements.
via Grails Best Practices.

Leave a Reply

Your email address will not be published. Required fields are marked *