Book Review: Grails in Action
Buy it now
One Minute Bottom Line
|A veritable Grails treasure chest. An absolute "must have", regardless of your level of Grails knowledge. You cannot possibly claim to be serious about Grails [or, dare I say it, web development in general] if you (1) don't already have this book or (2) aren't planning to buy this book first thing tomorrow morning.|
Part 1: Introducing Grails
- "Chapter 1: Grails in a hurry...":
You learn what the big ideas in Grails are: convention over configuration, agile philosophy, rock-solid foundation, scaffolding and templating, Java integration, incredible wetware, and productivity ethos.
- Getting started and the Hello World story, gets your feet really very wet: creating a quote of the day application. For this, you create a controller, then a view, a Grails layout, followed by a domain and actions. By the end of this first chapter, you've even integrated AJAX functionality into the application.
- Example tip from the end of the chapter: "Rapid iterations are key. The most important take-away for this chapter is that Grails fosters rapid iterations to get your application up and running in record time, and you'll have a lot of fun along the way."
- "Chapter 2: The Groovy essentials":
The obligatory chapter in Grails books to introduce newbies to Groovy. Some books put this section in the back, as an appendix, but I prefer it here. Groovy shouldn't be hidden under a bushel!
- Covers Groovy in a very good way, covering all the standard fare (syntactic sugar and so on), as well as the very few Java constructs that are not supported by Groovy.
- The tips at the end of this chapter are handy: Use idiomatic Groovy, Experiment, Use methods (instead of closures) where appropriate, Use explicit types in method signatures. Also, remember that Groovy isn't simply a language for Grails, but also for testing, scripting, and much more.
Part 2: Core Grails
From pages 63 to 217, you learn the structure of basic Grails applications. If the book were to contain nothing other than these pages, you'd have your money's worth.The sequence of chapters begins by spending a lot of very well spent time on the domain, followed by a chapter on the controller, with the next chapter dealing with the view. The final chapter of this part deals with testing. (Great to have that within the "Core Grails" part!) Plus, the sample application, covering various parts of several chapters, is started from the very beginning of this section.
- "Chapter 3: Modeling the domain":
Opening paragraph, reproduced here to show how reassuring and kind the tone of the whole book is: "In this chapter, we'll explore Grails' support for the data model portion of your applications, and if you're worried we'll be digging deep into outer joins, you'll be pleasantly surprised. We won't be writing a line of SQL, and you won't find any Hibernate XML mappings either. We'll be taking full advantage of the Convention over Configuration paradigm, which means less time configuring and more time getting work done."
- The concept of "domain-driven design" is introduced here and illustrated very well by means of an example. Validation via the constraints closure in the domain class is discussed in detail, including custom validation and cross-field validation. Also clarified are the relationships between constraints and the entries generated in the database by Grails. As also done in the Grails Refcard, 1-1, 1-m, and m-n relationships are outlined and explained in detail, again specified in the domain classes, using Groovy.
- An excellent explanation of GORM and its place in the world: "Object relational mapping (ORM) is the process of getting objects into and out of a relational database. It means that you can (mostly) be oblivious to the SQL that's happening behind the scenes. In Java applications, that role is usually handled by an ORM like Hibernate or JPA; in Grails, it's done by GORM, which takes full advantage of Grovy's dynamic typing to make data access simple."
- The validators table on page 77 would have been even better if it had been duplicated in a reference section at the end of the book. The Grails Refcard has a similar table on page 3, but without the handy examples and error properties that this table contains. However, the table in the Grails Refcard indicates that there are more validators than are shown in this book, where there is a reference to an on-line location of the complete list, which would have been better in a reference section at the end of the book.
- Example tip from the end of the chapter: "Learn the basic modeling options well. You'll spend a lot of time setting up Grails models in your future development work. Take the time to learn all the basic relationship types presented in this chapter. The test cases will give you valuable experimentation fodder."
- "Chapter 4: Putting the model to work":
A brilliant chapter. Here you learn the many ways in which the domain can be used in Grails applications, justifying the term "domain-driven design".
- You are shown how the UI is generated from the domain and you also learn how to customize the error messages displayed in the UI (which are shown depending on the constraints closure in the domain class). After introducing dynamic scaffolding, you're shown how to generate the files that define the scaffold, so that you can customize them.
- The dynamic finders section is very practical and you can play with the code immediately, which again underlines the power of the domain in the Grails world, since the dynamic finders are generated on the fly from the domain classes.
- The closing section on bootstrapping, for populating a database with some test data, should be an eye opener to anyone who doesn't know about this Grails feature.
- Example tip from the end of the chapter: "Use scaffolds for instant gratification and to stay motivated. Scaffolds give you a good feeling of progress and keep you focused on getting your app out the door. Also, don't be afraid of using scaffolding code for admin screens in production code."
- "Chapter 5: Controlling application flow":
The chapter on controllers. It covers—controller essentials, services, data binding, command objects, working with images, intercepting requests with filters, and custom URL mappings.
- Good to see services discussed in the same chapter as controllers. It took me a long time to understand the role of services, which this chapter covers well: "In this section, we'll extract all of our new-post functionality into a Grails service that we can call from anywhere we like. It will make things a whole lot tidier and more maintainable."
- This is the first point in the book where data binding is discussed. Very good job is done, as usual, starting from a code snippet and then drawing general principles from that point.
- I learned about command objects for the first time (section 5.4): "The command object's purpose is to marshal data from form parameters into a non-domain class that offers its own validation." (For example, and this is the example used here, this could be useful for validating a 'password' field with a 'confirmPassword' field.)
- Techniques for dealing with images. Brilliant and practical as always. "Handling file uploads", "Uploading to the filesystem", and "Rendering photos from the database".
- You are introduced in some detail to "/grails-app/conf/UrlMappings.groovy". "This file is where you configure rules for routing incoming requests to particular controllers and actions."
- Many very subtle techniques are discussed in this chapter. I need to spend a lot of time onall the tips and tricks here. Many new terms (would also have been good to have a terminology section at the end of the book)—flash scope, command objects, permalinks, wildcard support, filters (which you can use before/after a controller action is fired), whitelists, blacklists, and several others.
- "Chapter 6: Developing tasty views, forms, and layouts":
- Custom tags. Section 6.2 shows how you can create custom tags to supplement those provided by Grails. You're also shown (page 167) how to change the namespace from the default g: to whatever you want it to be. (Again, practical little tip: "It's best to use short namespaces to reduce typing.")
- The discussion (6.3.1) on page decoration via SiteMesh is very interesting, showing how to set up custom page layouts. Skinning and navigation tabs are described too.
- "Chapter 7: Building reliable applications":
The chapter on testing. It covers—unit testing, integration testing, and functional testing.
- Good introduction to testing in general, leading up to a very strong endorsement of test-driven development. Mock objects are also covered (7.2.3).
- Excellent—you are shown how to unit test each part of your Grails application. Subsections focus on (7.2.1) domain classes, (7.2.2) services, (7.2.4) controllers, and (7.2.5) tag libraries.
- The use of several tools and plugins are included—functional test plugin, Selenium, JsUnit, and fixtures.
- Example tip from the end of the chapter: "Test at all levels. We can't stress this enough: make sure that you're testing at both the unit and functional levels. Even if there is some overlap and redundancy, that's better than having gaps in your test coverage."
Alternative view technologies, especially Wicket, might have been discussed, either here or elsewhere in this book. Personally, I've drunk the Wicket juice and believe, along with other Wicket believers, that code should not be mixed inside the view layer, which is what JSP and GSP are both guilty of. I know there's a Wicket plugin for Grails and would have been interested in having a thorough introduction to how to integrate it somewhere in this book. The AJAX component model that Wicket provides is one area where the Wicket approach might be preferable to the heavy tagging (i.e., further embedding of code into the view layer) imposed by the way Grails handles AJAX.
Part 3: Everyday Grails
This part excellently covers exactly those topics I wanted to learn about after reading the previous chapters. I.e., the questions I was then left with were: "What about autocompletion, charts, and similar features?", "What about step-by-step forms, i.e., how to transfer data between pages?", "What about security?", "What about REST?" In addition to these topics, there is also a very interesting discussion about the integration of JMS and Quartz, via the respective plugins.
- "Chapter 8: Using plugins: adding Web 2.0 in 60 minutes":
Some plugins have already been referenced or used earlier in the book. Here, though, we get a first look at the plugin architecture of Grails applications. After the architecture and installation explanations, very practical explanations are provided on adding charts and graphs, as well as mail support. Ten pages on adding a full text search feature, as well as a bunch of typical UI features, such as tooltips, autocompletion, and calendars, are included as well.
- Charts and graphs. Starts with a nice intro "Creating your first chart", which really shows how to get started with charts, from scratch. And, before you know it, you've created a 3-D pie chart, a Google bar chart, and a multiseries line chart.
- GrailsUI plugin discussed in detail: "There are numerous Grails plugins for adding more UI sizzleto your app, but one of the most popular (and easy to use) is the GrailsUI plugin." Via this plugin, you're introduced to tooltips, rich-text editing, calendar-style dates, and autocomplete.
- A tip at the end of the chapter: "Explore GrailsUI. The GrailsUI plugin has a wealth of custom components you can use. You know all the basics now, so adding new components to your library will be easy. Take the time to browse the GrailsUI documentation to see what UI features are available."
- "Chapter 9: Wizards and workflow with webflows":
Here's the problem statement solved in this chapter: "What if you have a signup process that spans several pages? Or what if your shopping cart checkout needs to optionally give the user shipping preferences based on their location? You can solve these issues with your current knowledge of controllers and a bunch of redirects with hidden form variables, but you'd be writing your own state engine, and dependending on the complexity of the flow, things can get complex quickly."
- Covers the way Grails handles web flows, in a lot of detail, with many code snippets. Yet another great and succinct definition: "A Grails webflow is a series of logical steps displaying various screens through which the user progresses to arrive at a final destination."
- Many practical tips & tricks throughout this chapter. I.e., when to use webflows, but also when NOT to use webflows. Inevitably, since the book focuses so strongly on testing, there's even a discussion (section 9.4) entitled "Testing webflows".
- Very good code snippet (page 258) is analyzed and expanded upon in a lot of detail to explain how webflows work. And how about this code snippet for an apposite example: "Invoking a stateful service to validate the card." The code snippets throughout this chapter, as everywhere else, are excellent.
- "Chapter 10: Don't let strangers in - security":
The chapter on testing. It covers—unit testing, integration testing, and functional testing.
- All the view tags for a custom login form, on page 300, is yet another example of how directly usable this book is.
- Plugins discussed—Authentication plugin, JSecurity plugin, and Spring Security (Acegi) plugin. Also JAAS. However, the focus is on the Spring Security plugin, with the first step how to install it and then you integrate it very quickly and easily into your application.
- A tip at the end of the chapter, about obfuscation: "Obfuscation isn't a substitute for proper security. Hiding or mangling information can be useful—after all, there's no point in advertising anything that might help an attacker. But if you rely on obfuscation, you run a high risk of compromise. It's complementary to other techniques, not a substitute."
- "Chapter 11: Remote access":
The chapter focuses mainly on REST. Then, it also shows RMI (via the remoting plugin) and SOAP. As usual, a long list of very useful best practices closes the chapter. Nice segway from previous chapter: "Now that you have a good grounding in securing an application from attack and unauthorized access, it's time to look at how other software (other than web browsers) can use and interact with your Grails applications."
- Over 20 pages on REST so set you on the right track—choosing resource IDs, providing access to resources, serializing & deserializing, as well as a section on testing your REST API.
- Thorough introduction to the Grails Remoting plugin, including a comparison of remoting protocols, RMI, HTTP invoker, and Caucho's Hessian & Burlap. The next section discusses SOAP, via the Grails plugins for Axis.
- Example tip from the end of the chapter: "Put some thought into your URLs. Good URLs are long-lived, consistent, and easy to understand. Although poor URLs are unlikely to have an impact on your API feature-wise, they can make life harder for those using it. If you do that, users will not bother using it. You can find plenty of articles on-line about good URL design."
- "Chapter 12: Understanding messaging and scheduling":
The chapter on JMS (messaging) and Quartz (scheduling).
- Nicely practical focus for the messaging story: "In this chapter, we'll add a messaging system to our application so we can create a link between it and Jabber, a popular instant messaging system." Similarly, here's the focus in the scheduling part of the chapter: "Kicking off a daily backup? Sending out daily digest emails? Regenerating your full text index? Every developer needs to deal with these kinds of scheduled events occasionally."
- Excellent introduction to messaging in general, covering a lot of common terminology, such as 'products','consumers', 'topics', and 'queues'. Nice diagrams too.
- Covers everything you'll want to know about messaging and scheduling—with heaps of code snippets again—plus the usual tips and tricks at the end of the chapter. Very clear as always: "Use queues when you want a message to be processed by one listener, and use topics for broadcast scenarios."
Part 4: Advanced Grails
- "Chapter 13: Advanced GORM kung fu":
Deals with tuning your queries to run more efficiently, query-caching options, and the refactoring of domain classes. Performance and scalability of your application, where it relates to GORM is handled in detail here. The P6Spy plugin and JMeter are installed and used to analyze query timings, handy when preparing for getting overloaded by queries as the application gets bigger and more popular.
- "Chapter 14: Spring and transactions":
Bit of a deepdive into the underpinnings of Spring, beneath the Grails framework. I have a feeling that one should never actually need to go this far, in typical cases, but that's why this is part of the advanced part of the book so it's good to have there. I'm not advanced enough at this point to really get a lot out of this chapter, though I'm sure I'll get back to it later.
- "Chapter 15: Beyond compile, test, and run":
The chapter that focuses on influencing and extending the build cycle. Integration with existing build tool, as well as Grails' own build system, i.e., Gant is introduced.
- The reason given for the existence of Gant seems a bit tenuous. After stating that Grails could have used one of the existing build tools, requiring you to download Ant or Maven and then set it up, page 417 explains: "that would go against the Grails philosophy of providing everything you need out of the box (except Java)!" I can't help but think that he reason for Gant is simply that it provides the developer with a Groovy approach to Ant scripting.
- Section 15.1.2 is great, teaching you step by step how to create a brand new Grails command "grails dist", which creates a distribution for your Grails applications.
- Section 15.2 is excellent in catering to those who have already standardized on Ant or Maven, meaning that the Gant system cannot be adopted in such cases. The Maven Grails archetype is introduced in 15.2.2, while the previous subsection uses Ant and Ivy instead of Gant as your build tool.
- "Chapter 16: Plugin development":
Everything you need to know about creating your own plugins that extend the features offered by the Grails framework to your applications—creating your first plugin, publishing & testing your plugin, and integrating with the Grails infrastructure.
- Didn't know this before: you can run a plugin using 'run-app', just like a standard Grails application. Also: "The most visible difference between a plugin and an application is the presence of a plugin descriptor, the SecurityGrailsPlugin.groovy file."
- "Integrating with Grails" covers a lot of interesting material—adding your own dynamic methods, handling class reloading at runtime, cofiguring your own Spring beans, adding entries to the application's web.xml (i.e., dealing with servlets and filters), and implementing extra commands.
- Example tip from the end of the chapter: "Use local plugin repositories for teams. Local plugin repositories serve several purposes. First, they allow you to control which versions of plugins are installed by your team by default. Second, they allow you to customize public plugins foryour own purposes. Last, they can be useful for modulerizing applications."