QCon 2007 Notes: Grails

Guillaume Laforge, Grails: Spring + Hibernate development re-invented

  • Key principles of Grails:
    • CoC (Convention over Configuration)
    • DRY (Don’t Repeat Yourself)
  • CoC model objects: POGOs (Plain Old Groovy Objects)
    • automatically mapped to DB, without mapping files or hibernate.cfg.xml
    • set relationships between model objects with def has_many, def belongs_to
    • model objects can have constraints added, which are then used all the way through, from front end to database, e.g.:
Author
    static constraints = {
        firstName (blank:false),
        email (email:true)
    }

or

isbn (matches:{regex})
  • Looks quite Rails-ish in terms of scaffolding and quick code
  • Dynamic finder methods, again on model objects, without needing to write any dao code, e.g.:
    • Book.findByTitleLikeOrReleaseDateLessThan(“%Grails%”, someDate)
    • Book.findByReleaseDateBetween(startDate, endDate)
    • Book.findAllByAuthor
    • also does sorting
    • for more complicated queries, there’s a DSL around Hibernate’s Criteria building
  • Services can automatically be injected into controllers by declaring them as fields: no Spring applicationContext.xml to maintain
  • By convention, any cron jobs in a given directory will be run automatically.
  • View is Spring MVC under the hood
    • There is very simple dynamic taglib generation (again, no tld, just add the tag in the right place and everything will be able to use it)
  • The sweet spot of Grails is enterprise-readiness: you can reuse
    • Java libraries
    • Employee skills
    • Spring beans
    • Hibernate mapping files for legacy objects (you won’t need to create them for new objects, of course)
    • EJB3 annotated beans, even
    • In fact, even if your model classes are in Java, you can still use the “magic” find methods

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply