Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

A Case for Grails


Published on

Published in: Technology
  • Be the first to comment

A Case for Grails

  1. 1. (A case for) GRAILS<br />Mark Daugherty<br />CITYTECH, Inc.<br />July 7th, 2010<br />
  2. 2. Outline<br />Inspiration<br />Grails overview<br />Comparison of Java/Spring/JPA application to Groovy/Grails<br />
  3. 3. Context<br />February 2010 : wrote a small Groovy/Grails app to learn the technologies<br />March 1st, 2010 : started on CDGA project (Struts, Spring, JPA)<br />March 1st, 2010 (later that day) : realized that CDGA had an enormous amount of boilerplate code that Grails could eliminate<br />
  4. 4. (Additional) Context<br />May 7th, 2010 : wrote CITYTECH blog post about code savings from replacing Spring DAO code with Grails<br />May 8th, 2010 – present : blog is read by a total of 4 CITYTECH employees<br />July 7th, 2010 : Grails begins to learn at a geometric rate. It becomes self-aware at 2:14 AM, Eastern time.<br />
  5. 5. Grails Primer<br />Web application framework built with Groovy (Java-based dynamic language) on top of proven Java technologies<br />Spring MVC, Hibernate<br />Full stack for rapid development<br />Embedded Tomcat container, HSQLDB, (G)Ant, scaffolding features<br />Get a basic app running within minutes<br />
  6. 6. You may notice…<br />Convention over configuration<br />Little-to-no initial configuration<br />Project structure is predefined according to best practices<br />Command line scripts for common tasks<br />Create new application, compile, test, run<br />Generate domain classes, controllers, services, test cases<br />Install plugins<br />Contrast with your last Maven-built project<br />
  7. 7. In practice…<br />
  8. 8. Simple Domain<br />Delegate 1  ∞ User<br />Delegate 1  ∞ Application<br />A “delegate agency” has one-to-many users and one-to-many applications. An application contains various forms, input by users, to collect data about the delegate agency.<br />
  9. 9. JPA (Java Persistence API)<br />@Entity<br />@Table(name = "USER")<br />public class User implements Serializable {<br /> @Id<br /> private Integer id;<br /> @OneToOne(fetch = FetchType.LAZY)<br /> @JoinColumn(name = "DELEGATE", referencedColumnName = "ID")<br /> private Delegate delegate;<br /> // getters and setters, equals(), hashCode(), toString()…<br />}<br />
  10. 10. (More) JPA<br />@Entity<br />@Table(name = "DELEGATE")<br />public class Delegate implements Serializable {<br /> @Id<br /> private Integer id;<br /> @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)<br /> private Set<User> users;<br /> @OneToMany(cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)<br /> private Set<Application> applications;<br /> // getters and setters, equals(), hashCode(), toString()...<br />}<br />
  11. 11. (Still more) JPA<br />@Entity<br />@Table(name = "APPLICATION")<br />public class Application implements Serializable {<br /> @Id<br /> private Integer id;<br /> @ManyToOne(cascade = { CascadeType.REFRESH }, optional = false)<br /> @JoinColumn(name = "DELEGATE”)<br /> private Delegate delegate;<br /> // getters and setters, equals(), hashCode(), toString()...<br />}<br />
  12. 12. Grails<br />class User {<br /> static belongsTo = [delegate:Delegate]<br />}<br />class Delegate {<br /> static hasMany = [users:User, applications:Application]<br /> static mapping = {<br /> users lazy:false<br /> applications cascade:'delete'<br /> }<br />}<br />class Application {<br /> static belongsTo = [delegate:Delegate]<br /> static mapping = {<br /> delegate cascade:'refresh'<br /> }<br />}<br />
  13. 13. DAO<br />JPA/Hibernate = verbose, repetitive<br />Even a small domain model can have 1000’s of lines of code for basic CRUD operations<br />Tedious to maintain<br />
  14. 14. GORM!<br />Hibernate under the hood<br />Groovy + runtime code synthesis to eliminate boilerplate code<br />
  15. 15. Basic CRUD<br />
  16. 16. Dynamic Finders<br />
  17. 17. Additional GORM Features<br />ORM DSL for mapping to legacy DB schemas<br />Custom mappings for non-standard table and column names<br />Event handlers (beforeInsert, afterUpdate)<br />Customizable caching and fetching strategies, transactions<br />
  18. 18. Web<br />Render XML or JSON responses using markup builders<br />Automatic marshalling of domain classes to XML/JSON<br />
  19. 19. (More) Web<br />GSP<br />Similar to JSP, but with better tag libraries<br />findAll, grep (filter) for collections<br />Method calls<br />Templates<br />Sitemesh (decorator) layouts<br />Customizable URL mappings<br />
  20. 20. Validation<br />Often painful, less so with Grails<br />
  21. 21. More Features<br />Spring Web Flow<br />Supports subflows, conversation scope (similar to Seam)<br />Interceptors / Filters<br />AJAX<br />DOJO, GWT plugins<br />
  22. 22. Even more to like<br />Groovy tests<br />Ideal for TDD<br />Unit tests created automatically from command line script<br />Dynamic language features for easy mocking<br />Easy RESTful web services<br />URL mappings<br />Controllers can render XML or JSON<br />Consider attending a CGUG meeting<br />Extremely active community<br />400+ plugins (Spring Security, JQuery, GWT, Google App Engine, Flex)<br />Wide range of tutorials<br />Frequent releases<br />
  23. 23. Summary<br />Grails compared to Spring, EJB, JPA<br />Substantially less code without compromises<br />Easier to maintain and test<br />Shorter iterations and release cycles<br />Same proven underlying technologies<br />Gain competitive edge<br />Why not?<br />