introduction to grails 2013

27
Introduction to Grails Spring, Hibernate and more, while doing less Gavin Hogan – Software Architect, CommerceHub

Upload: gavin-hogan

Post on 10-May-2015

625 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Grails 2013

Introduction to GrailsSpring, Hibernate and more, while doing less

Gavin Hogan – Software Architect, CommerceHub

Page 2: Introduction to Grails 2013

Gavin HoganA little bit about me.

• Software Architect at CommerceHub• Working with Java and Groovy for 8 years• Working with Grails for 3 years

Page 3: Introduction to Grails 2013

Agenda

• Grails in a nutshell• Some Grails patterns and philosophy• Spring in Grails• Object Persistence• Grails web technologies• Grails plugin ecosystem• A closer look at groovy• Live demo• Q&A

Page 4: Introduction to Grails 2013

Grails in a Nutshell… and a little history

• Grails is a rapid web application development framework

• Grails is a full stack framework, not simply a web development framework

• Grails Leverages Groovy and Spring extensively

• Modular and extensible though the plugin infrastructure

Page 5: Introduction to Grails 2013

Grails in a Nutshell… and a little history

• Backed by SpringSource/VMWare• Grails is mature, project started in 2005,

current version is 2.2• Inspired by Ruby on Rails. An RoR

influence remains, but it is not a clone.

Page 6: Introduction to Grails 2013

Grails Patterns & Philosophy

• Grails was inspired by RoR. Ease of development is a primary objective

• Leverage convention over configuration with sensible defaults

• Never eliminate flexibility or capability for the price of simplicity

• All the power of Spring, Sitemesh, Hibernate is available, but common use case are pre configured

Page 7: Introduction to Grails 2013

Grails Patterns & Philosophy

• Leverage existing operational capacity and expertise. 'Plain old java deployment’

• Portable war based deployment• Apache Tomcat is the default container

• ‘Standing on the shoulders of Giants’, Java compatibility allows the use of existing industrial strength components.

Page 8: Introduction to Grails 2013

Spring in Grails

• Grails really is ‘Spring in a can’• Most grails applications do not have any

Spring xml!• Grails automates the configuration and

'wiring' of Spring beans• Name based ‘convention over

configuration’• UserService.groovy will automatically

generate a bean named ‘userService’• Object fields are autowired by name

Page 9: Introduction to Grails 2013

Spring in Grails

• Plugins and applications can interact with Spring lifecyles.

• Controller layer is a wrapper around Spring MVC.

• Spring extensions and subprojects are easy to include with plugins.• Spring Security• Spring Integration• Spring Batch• …

Page 10: Introduction to Grails 2013

Spring in Grails

• Plugins and applications can interact with Spring lifecyles.

• Controller layer is a wrapper around Spring MVC.

• Spring extensions and subprojects are easy to include with plugins.• Spring Security• Spring Integration• Spring Batch• …

Page 11: Introduction to Grails 2013

Object Persistance

• Grails Object Relationship Mapping (GORM) is the default persistence layer in a Grails application

• The default GORM implementation is a wrapper around Hibernate.

• Alternative implementations for MongoDB, Reddis, REST, SimpleDB and Riak exist

• Mapping is sensible by default.• Class names name to table names• Field names map to column names

Page 12: Introduction to Grails 2013

Object Persistence

• Dynamic finders methods are automatically added to your objects at run time• E.g. User.findAllByLastName('Hogan')

will return a list of users objects with that last name

• The finders support composite queries and are very capable. Criteria builders and named queries support more challenging requirements

• Mapping DSL, gives easy access to common configurations for Hibernate persistence

Page 13: Introduction to Grails 2013

Grails Web

• Controllers and controller actions are automatically mapped to urls based on name• UserController.list() is mapped to

user/list/• HTTP paramaters are automaticaly bound

to controller arguments• UserController.show(String id) is

mapped to user/show/<id>

Page 14: Introduction to Grails 2013

Grails Web

• Binding transparently applies to objects used as arguments in controller actions.• UserController.update(User u) is

mapped to <appName>/user/update/<id>?firstName=<firstName>&lastName=<lastName>

• Generally speaking you do not think about binding much when working with Grails

Page 15: Introduction to Grails 2013

Grails WebValidation

• Grails provides simple validation DSL for Domain and Command objects.

• Common validation constraints built in• Unique• Size• Email• Not Blank/Null

• Custom constraints easy to add directly to objects

Page 16: Introduction to Grails 2013

Grails WebValidation

• Validation failures generate errors, which are automatically associated with i18n message codes

• Validation is intended for domain and command objects but can be assigned to POJOs too.

• Grails validation is powerful, but does not provide a good business rules capability.

Page 17: Introduction to Grails 2013

Grails WebViews

• The default view technology for grails is 'Groovy Server Pages' (GSP)• Very similar to JSP, fewer rough edges.

• Views are automatically associated with actions based upon name

• UserContoller.list() assumes a list.gsp in /views/user/

• Templating is extremely easy, leverages Sitemesh.

Page 18: Introduction to Grails 2013

Grails WebTaglibs

• I swore to never use taglibs many years ago, Grails makes taglibs a joy

• They are simple reusable view components.

• Taglibs a written as regular groovy Closures, they can interplay with each other and the rest of the application code

• To create a taglib, simply create a class with ‘TagLib’ at the end of its name, all public closures will automatically become tags

Page 19: Introduction to Grails 2013

Grails PluginsWhat makes them better that jar files?

• Plugins are core to Grails. Grails itself is built as a collection of distinct plugins

• In Grails, plugins are the standard unit of reusable code.

• Plugins usually package behavior, not only capability. • E.g. Including the Shiro security plugin

actually secures/locks down your application.

Page 20: Introduction to Grails 2013

Grails PluginsWhat makes them better that jar files?

• Anyone can write a plugin. You can choose to share a plugin on grails.org

• The range of plugins is now vast• Though quality is varied• Suffers from the GitHub effect, “I can

share this so I will!”

Page 21: Introduction to Grails 2013

Jump Into Code

• Not yet…

• Lets talk about Groovy

Page 22: Introduction to Grails 2013

Groovy Primer

• Groovy is a dynamic JVM language• It compiles to java bytecode and runs in

the same jvm as Java code• Intended to be familiar/comfortable to

Java developers• Groovy provides additional, powerful

programing features, Closures, Mixins, Meta-Programming

Page 23: Introduction to Grails 2013

Groovy PrimerSensible simplifications

• Closures can be thought of as being similar to functions in JavaScript. You can pass them or call them.

• Mixins are used to compose complex object without inheritance. You may think of them as behavioral interfaces, that actually do stuff.

Page 24: Introduction to Grails 2013

Groovy PrimerSensible simplifications

• Meta-programming allows developers to modify the behavior of a class at compile-time or runtime. Grails extensively uses meta programming to add behavior to your application

• Groovy is not strongly typed, ‘def’ is similar to ‘var’ in JavaScript, and can be thought of as similar to Object in java.

• Some many enhancements to Collections API

Page 25: Introduction to Grails 2013

Some CodeBecause live coding always works as expected!

Page 26: Introduction to Grails 2013

Questions?

Page 27: Introduction to Grails 2013

Thanks

@[email protected]