About the Reviewer
David Sills's muse is fueled by Bach, Beethoven, and Brahms (and Berlioz and Boulez). He has no life, which comports well with the über-geekitude to which he aspires. He reads and writes fitfully in English, Spanish, French, German, Italian (mostly medieval), C, VB, Perl.... Oh, and Java and XSLT, lots and lots of Java and XSLT. Trained somewhere in darkest Central America, he works today at DataSource, Inc., on the product team for the CASE tool Jetson.
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.

The Definitive Guide to Grails

Author(s) Graeme Keith Rocher
Publisher Apress
PubDate 2006
Reviewer David Sills



One Minute Review

Positives
  • The first book to cover Grails in detail
  • Well-chosen examples useful in real life
  • Clear organization
  • Thorough coverage
Negatives
  • Really needs some familiarity with Groovy
  • Covers a lot for a medium-sized book
  • A slight tendency towards dryness

Sections

Intent & Audience

Grails is a web framework built upon the considerable success of Groovy and the development of other powerful open-source toolkits. It builds on the simplicity and power of Groovy as a language and upon the idea, made popular in Ruby among other languages, of "convention over configuration." Another appeal of Groovy is the ability to reuse so much functionality from Java, which after all is not going anywhere in the immediate future. Hibernate, Spring, SiteMesh, Quartz: all these are well-known and frequently-used tools, often combined together to produce applications. Each, however, has a significant learning curve on its own, one that Grails, by combining them in a standard way and at a far simpler level of abstraction, seeks to flatten.

The Definitive Guide to Grails is the first to present the exciting developments that have culminated in the latest versions of Grails. It seeks to present the range of options available in Grails to an audience already at least somewhat familiar to the features of modern web applications in Java. It will best be read with that sort of background. If you're familiar with the abovementioned frameworks and sigh just a little when you have to again work through the laborious details of configuring them each individually and all to work together, and breathe a sigh of relief when that's done and you won't have to revisit it too often before the next new app, Grails certainly deserves a look, and probably a lot more.

This book is also, secondarily, intended as a presentation of the possibilities inherent in Groovy when it is used to its best advantage. The author not only (necessarily) introduces Groovy as a language in Chapter 2, but frequently adds asides about the utility and elegance of the various syntactical features of Groovy of which he is making use. He makes the point that until recently, very little was properly documented about Groovy; with the appearance of several recent books on the language, Groovy's time is perhaps at hand. As a pleading for a language, this is indeed powerful stuff.

Relevance of material

This book is decidedly on target when it comes to presenting what you need to know to get up and running with Grails. From installation to interface, from architectural choices to interactions with other languages and applications, it's all here. The examples mostly work well (I never did actually get the automatic reloading of Groovy classes to work, but that may not have been Grails), and are exceptionally well chosen to present real-world problems and potential solutions. One would hardly want to build a real-world application without carefully consulting this book for terrific hints about "how it's done."

The wide coverage in this book is certainly to be commended, but there is a tradeoff in terms of the presentation of Groovy as a language. While Roche attempts to make this book independent, I'd say you'd do well to be familiar with Groovy before reading it. I can confidently suggest Groovy in Action, which I have also reviewed this week (see Resources), as an excellent place to start. Another might be Groovy Programming (see Resources).

Chapter highlights

Chapter 1 starts out discussing how J2EE and wound up configuration-heavy and over-complex. Other languages and frameworks with attractive features have developed in reaction to this, but Java isn't going away, so what's a developer to do? That's where Grails comes in. In honor of the brilliant Ruby on Rails project, Grails started out as Groovy on Rails, aiming to combine the simplicity that Groovy brings to Java development (about which see my other review this week) and the enormous strides that have been made in open-source frameworks like Hibernate, Spring, and others. Of course, these have not developed without adopting their own configuration woes. Grails seeks to knit these together seamlessly and make at least their typical uses far simpler than they would be without it. The remainder of the chapter is devoted to installation and baby steps with Grails.

Chapter 2 covers the Groovy language at the 50,000-foot level, too high, unfortunately, to be very useful. If the coverage is not deep, it is at least broad, and might serve as a useful brush-up should you need one.

Chapter 3 presents Grails as implementing a Model-View-Controller (MVC) paradigm. The directory structure and file contents are discussed, as are the Ant tasks that can be used to create them. The Grails Console and Shell are extended versions of their Groovy counterparts; some IDEs, especially Eclipse, offer fairly easy integration with Grails projects.

Chapter 4 introduces the application domain, using Grails Object-Relational Mapping (GORM), which in turn builds on the well-known Hibernate framework. Create-Read-Update-Delete (CRUD) operations, relationships between objects, and database queries are significantly simplified with Grails over against plain Hibernate, owing to the power of convention and of Groovy. Particularly fascinating is the application of Groovy's builder syntax to application domain constraints, which can be defined and validated against within the domain layer itself.

Chapter 5 discusses Grails's implementation of scaffolding, the term coined in Rails to describe generating a basic user interface building upon application domain operations. With a few lines of code, a simple, dynamic GUI can be created that will meet the most basic requirements. Of course, for more sophisticated operations or customized GUIs, static scaffolding can be generated and modified to suit. In all Grails scaffolding, a clear separation is maintained between the application domain model and the views and controllers that make up the scaffolding.

Chapter 6 - An old adage says "if it wasn't measured, it didn't happen." The software development equivalent of this might go "if it wasn't tested, it doesn't work." This chapter introduces testing in Grails, and the author's timing is impeccable - before this, there wasn't much to usefully test. Unit testing is naturally presented first, with Groovy's special takes, in the form of the GroovyTestCase with its added convenience methods, mocks and stubs, and the use of closure currying to define data. Canoo's WebTest framework is then called into action to test the scaffolding developed in the previous chapter. Grails generates, again, most of the framework code for web testing; pretty much all that remains is to actually fill in test data and run the tests.

Chapter 7 zooms in on the controller part of MVC in more depth. Controllers were introduced briefly in examples of Grails scaffolding, but were little more than a few lines of magic code. There is, of course, much more to the part of MVC that must react to user requests: controllers have to know how to deal with user-supplied data, including conversion of string parameters into non-string types; what to do with the data after it's collected and converted; and how all this relates to the overall flow of the application, including rendering a response. The idea of flash scope, Grails's addition to the scopes well known from Java servlets and JSP. is useful here: flash scope carries data over to the next user request, after which it vanishes. This is a typical usage, and flash scope is far easier to work with than session scope, which must be explicitly managed or data will hang around long after it's useful. Flash scope would make it straightforward to chain several controller actions together as a workflow, but even that effort is unnecessary: Grails adds an method to manage chaining without having to focus on the details. Another neat solution is provided to work with AOP: with the power of convention, adding interceptors is quick and easy. The book's example of using an interceptor to handle user login is inspired and surely to be emulated. Finally, a brief discussion of a common problem, handling file uploads, concludes the chaper. Once again, Grails provides a simple abstraction over the (moderate) complexity of Spring's handling of the problem, reducing this once tedious chore (I can certainly remember writing code to explictly parse a multipart request) to no more complexity than handling single parameter values.

Chapter 8 focuses on the views of MVC, presenting Groovy Server Pages (GSP). These present many constructs familiar from JSP: scriptlets; tag libraries (Grails, of course, provides its own useful versions, including dynamic tags, which can be called as methods within other tags); HTML form support; type conversion and data validation; and internationalization (i18n). Grails uses the SiteMesh framework to handle the high-level details of consistent rendering of views from layouts, and uses a template mechanism to provide the bits of markup that make up the content to be placed within these layouts. A terrific tag supplied by Grails is the paginate tag, which handles transparently the complex issue of paginating long lists of data. Of course, you can build your own custom tags, and with an example of this the chapter ends.

Chapter 9 discusses Ajax, the asynchronous Javascript technology making a great impact on web development recently. What is this doing in a book on Groovy and web applications? Grails (as by now one expects) provides an easy-to-use tag abstraction over several popular Ajax libraries, making them even more accessible to developers. Handling Ajax calls (including adding methods to be called before and after calls) and events is simplified and made consistent with other uses of GSP tags.

Chapter 10 presents the idea of a service layer, an architectural choice representing the face the application presents to clients. The service layer processes business logic, for instance, involving multiple domain objects, where putting this logic into domain objects might couple them unnecessarily, or more elaborate than should be appropriately placed in a controller, which should really coordinate application responses at a higher level. As usual, Grails provides a simple abstraction to Springs transaction and Inversion of Control (IOC) capabilities, allowing the developer to bypass the complex underlying configuration of Spring. Another feature Grails abstracts is the scheduling of jobs using the Quartz framework. Creating, testing, and maintaining jobs occupy the remainder of the chapter.

Chapter 11 discusses integration between Grails and Java. This, of course, is where Groovy shines over other scripting languages (I have commented on the adjective already - see my other review this week): Groovy can not only be used in the JVM alongside Java; Groovy is Java. Examples of Java integration involve explicitly using Hibernate and Spring, rather than relying on Grails's abstractions of their functionality; integration of Grails with EJB3; and integration with the XFire framework to provide web services using SOAP. Having thus reached out even beyond Java to other applications and languages, the book closes.

Rating

You know you're really going to like a book when you learn more from its first page than you typically do from the first chapter of other, similar books. That was my experience with The Definitive Guide to Grails. (It is true that I did follow up on the footnotes, but in this case, one should.) The rest of the pages didn't disappoint, either.

The text is especially well written to live on your shelf as reference material - there is very little duplication of content from one area of the book to another. Because of this, however, the discussion of a topic once it arises is extremely dense, and the author's linguistic felicity is not always sufficient to control a certain dryness than can creep in. Style, however, is not the only criterion, and it has to be said that Rocher has worked hard to cover an awful lot of ground, and has done remarkably well at doing so. The Definitive Guide to Grails is entirely to be recommended; if you're at all interested in Grails, this is a must-have.

Relevance
Readability  
Overall  

Resources

Grails: http://grails.codehaus.org/

Groovy: http://groovy.codehaus.org/

This book's website: http://www.apress.com/book/bookDisplay.html?bID=10205

The website for Groovy in Action (see review below): http://www.manning.com/koenig/

The website for Groovy Programming: http://books.elsevier.com/us//computerscience/us/subindex.asp?maintarget=&isbn=9780123725073

The Grails mailing lists: http://grails.codehaus.org/Mailing+lists

Groovy in Action review at Javalobby

Purchase The Definitive Guide to Grails on Amazon and help Javalobby.