Chapter 7. Services and data binding

 

This chapter covers

  • Using services for more maintainable architecture
  • Data binding and error handling
  • Using command objects for tricky form validations
  • Uploading and rendering images
  • Working with URL mappings and filters

In the previous chapter you learned the basics about what controllers do, how to extend them with your own logic, and how to test them. We haven’t yet introduced you to the best way to use controllers in building larger Grails applications.

In this chapter we examine the controller’s best friend: the Grails service. Services help free controllers from the heavy lifting of application logic, and let controllers do what they do best—control the flow of the user through the application. In this chapter we’ll take you through all the basics of Grails services, and provide insights to grow your application in a maintainable and testable way.

And if we free controllers from the tyranny of heavy business logic and return them to their flow controlling core business, it’ll give us a reason to discuss taking data from an HTML form and binding it into variables and objects that you can process more conveniently in your application. Grails calls this operation data binding because you’re binding form data to real Grails objects. In this chapter we’ll take you through the various mechanisms for Grails data binding and show you when it’s best to use each one.

7.1. Services: making apps robust and maintainable

7.2. Data binding

7.3. Command objects

7.4. Working with images

7.5. Intercepting requests with filters

7.6. Creating custom URL mappings

7.7. Summary and best practices