SlideShare a Scribd company logo
Groovy & Grails
Alex Staveley
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
• Groovy
– 2003 (1.0 2007)
– Dynamic Language (Ruby, Python) (Expressive, Ceremony, syntactic
sugar)
– JVM
– JStack, JPM, javap, jconsole
• Grails
– Web Framework
Page 3
Groovy & Grails - History
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 4
History
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 5
Start with Groovy …
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 6
Start with Groovy (cont.)
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 7
Start with Groovy (cont.)
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 8
Start with Groovy (cont.)
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
String country = country.name ?: “Unknown Country”
String location = map ?.getLocation()?.getCoordinates()
[‘Denver’, ‘Paris’, ‘Nice’, ‘Lima’]*.toUpperCase()
//
sendRequest(QkrUser.&create)
…
sendRequest(MethodClosure method) {
…
method(“new user”)
Page 9
Fancy operators
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 10
Express a regular expression
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
[[sku:12, name:’walmart’], [sku:78, name:’walmart’], [sku:54, name:’walmart’],
[sku:88, name:’saks’],[sku:33, name:’saks’]]

[walmart: [12, 78, 54], saks: [88, 33]]
Map productSkusPerMerchant =
skuMerchantPairs.groupBy{it.merchantName}.collectEntries{k, v -> [(k): v*.sku]}
Page 11
Very fancy collection stuff
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 12
Fancy other stuff
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 13
Fancy String Stuff
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
String message = “Dublin is better than most places”
println “First Character: ${message[0]}” // D
println “Last character: ${message[-1]}” //s
// sub-string
println message[0..5] // Dublin
println message[5..0] // nilbuD
Page 14
More fancy stuff
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
def “Test findPositives” () {
given: “A bunch of people”
def service = new NumberService()
when: “I pass in a list of numbers”
def result = service.findPositives(numbers)
then: “Only positive numbers returned”
result == expected
where:
number | expected
null | []
[-1, 4, 5] | [4, 5]
Page 15
Really good testing opportunities – even for Java
projects
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 16
Closures
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 17
Closure - JavaScript
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 18
Closures
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 19
Closures – Execute Around Pattern
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 20
Execute and Around Pattern cont.
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 21
Meta Programming
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
• @Immutable
• @TypeChecked
• @GrailsTypeChecked
• @CompileStatic - slow groovy code.
• @Delegate
Page 22
AST Transformations
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 23
@Delegate
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 24
AST Transformation write your own
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 25
AST Transaction, write your own
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 26
CartItem with Updated annotation
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 27
Great JSON / XML support
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 28
Great XML support cont.
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 29
JSON Support
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
trades = {
buy 500 of ryanair
sell 300 of microsoft
buy 100 of google
}
trades = {
buy(500).of(ryanair)
sell(300).of(microsoft)
buy(100).of(google)
}
Page 30
DSL
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
- Rapid Web Application Dev Framwork
- First Class Java Integration
- Inspired by Ruby on Rails, Django
- Convention over configuration
- Leverages Java technologies Spring, Hibernate
- Spring MVC under the hood
- Service container
- Full stack
- Build in DB
Page 31
Grails
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
• grails –version
• grails create-app MyApp
• grails run-app
• grails console
– Very useful for understanding GORM, consequences etc.
Page 32
Grails command line
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 33
Grails – Convention over Configuration
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 34
Grails Configuration – really good
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 35
Grails - BuildConfig
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 36
Grails - Plugins
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
• Hibernate, Mongo, Redis etc
• Put in the domain package and you get 20 instance methods and
over 70 static methods
• Instance: delete(), save(), validate(), refresh()
• Static: count(), find(), findAll(), first(), last()
– Note methods such as findByNameI) etc are synthesised code
• Build in constraints: creditCard, email, unique (executes a query
string during validation),
Page 37
Grails - GORM
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
Page 38
GORM Example
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
• How good is your schema?
• Saves can silently fail (failOnError)
• Saves happen in the “Hession”. Hibernate decides when they get
fired to DB and in what order (flush is false by default)
• Changes to objects attached to the session persist, without a call to
save
• Cascading saves / deletes can take a bit of time to get right.
• N + 1 problem
• Just enable hibernate logging
Page 39
GORM Gotcha’s
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
• Perl, Python not on the JVM
• Bash – remember, forget, can be difficult to read.
• Anywhere the JVM is, you can run the script.
• Define classes in your script
• @Grab annotation to get dependencies from maven central
• Run easily from command line
A great use case for Grails / Groovy
Page 40
Grails – Scripts
©2014 MasterCard.
Proprietary and Confidential
October 30, 2014
• Basics – any Java programmer. Experience of Python, JavaScript,
even Scala etc. helps
• Best practises
– ShopThis!
• Code reviews
Page 41
How do we learn it?
© Copyright – Internal Use Only

More Related Content

Viewers also liked

BROCHURE DE PRODUCTOS NTH GLOBAL
BROCHURE DE PRODUCTOS NTH GLOBALBROCHURE DE PRODUCTOS NTH GLOBAL
BROCHURE DE PRODUCTOS NTH GLOBAL
NHT Global
 
Valura гель для мужчин от NHT Global
Valura гель для мужчин от NHT GlobalValura гель для мужчин от NHT Global
Valura гель для мужчин от NHT Global
goldenmouse
 
Fibe rich клетчатка для Здорового кишечника
Fibe rich клетчатка для Здорового кишечникаFibe rich клетчатка для Здорового кишечника
Fibe rich клетчатка для Здорового кишечника
goldenmouse
 
EL CLIMA DESÉRTICO
EL CLIMA DESÉRTICOEL CLIMA DESÉRTICO
EL CLIMA DESÉRTICO
aarasha013
 
10 tips para tu entrevista en IT
10 tips para tu entrevista en IT10 tips para tu entrevista en IT
10 tips para tu entrevista en IT
Belatrix Software
 
Tu dices lo que eres
Tu dices lo que eresTu dices lo que eres
Tu dices lo que eres
Presentaciones PowerPoint.com
 
Edith piaf
Edith piafEdith piaf
Edith piaf
Priscilla Reina
 
Supply Chain and Logistics
Supply Chain and LogisticsSupply Chain and Logistics
Supply Chain and Logistics
Luis A. Luyo
 

Viewers also liked (8)

BROCHURE DE PRODUCTOS NTH GLOBAL
BROCHURE DE PRODUCTOS NTH GLOBALBROCHURE DE PRODUCTOS NTH GLOBAL
BROCHURE DE PRODUCTOS NTH GLOBAL
 
Valura гель для мужчин от NHT Global
Valura гель для мужчин от NHT GlobalValura гель для мужчин от NHT Global
Valura гель для мужчин от NHT Global
 
Fibe rich клетчатка для Здорового кишечника
Fibe rich клетчатка для Здорового кишечникаFibe rich клетчатка для Здорового кишечника
Fibe rich клетчатка для Здорового кишечника
 
EL CLIMA DESÉRTICO
EL CLIMA DESÉRTICOEL CLIMA DESÉRTICO
EL CLIMA DESÉRTICO
 
10 tips para tu entrevista en IT
10 tips para tu entrevista en IT10 tips para tu entrevista en IT
10 tips para tu entrevista en IT
 
Tu dices lo que eres
Tu dices lo que eresTu dices lo que eres
Tu dices lo que eres
 
Edith piaf
Edith piafEdith piaf
Edith piaf
 
Supply Chain and Logistics
Supply Chain and LogisticsSupply Chain and Logistics
Supply Chain and Logistics
 

Similar to Grails

Drawbridge_MeetUp_June19_072414
Drawbridge_MeetUp_June19_072414Drawbridge_MeetUp_June19_072414
Drawbridge_MeetUp_June19_072414
Nitin Panjwani
 
NYC HUG - Application Architectures with Apache Hadoop
NYC HUG - Application Architectures with Apache HadoopNYC HUG - Application Architectures with Apache Hadoop
NYC HUG - Application Architectures with Apache Hadoop
markgrover
 
Getting Started with CoreGraphics
Getting Started with CoreGraphicsGetting Started with CoreGraphics
Getting Started with CoreGraphics
Xamarin
 
The Modern Java Web Developer - Denver JUG 2013
The Modern Java Web Developer - Denver JUG 2013The Modern Java Web Developer - Denver JUG 2013
The Modern Java Web Developer - Denver JUG 2013
Matt Raible
 
20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...
20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...
20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...
LinDa_FP7
 
How we lose etu hadoop competition
How we lose etu hadoop competitionHow we lose etu hadoop competition
How we lose etu hadoop competition
Evans Ye
 

Similar to Grails (6)

Drawbridge_MeetUp_June19_072414
Drawbridge_MeetUp_June19_072414Drawbridge_MeetUp_June19_072414
Drawbridge_MeetUp_June19_072414
 
NYC HUG - Application Architectures with Apache Hadoop
NYC HUG - Application Architectures with Apache HadoopNYC HUG - Application Architectures with Apache Hadoop
NYC HUG - Application Architectures with Apache Hadoop
 
Getting Started with CoreGraphics
Getting Started with CoreGraphicsGetting Started with CoreGraphics
Getting Started with CoreGraphics
 
The Modern Java Web Developer - Denver JUG 2013
The Modern Java Web Developer - Denver JUG 2013The Modern Java Web Developer - Denver JUG 2013
The Modern Java Web Developer - Denver JUG 2013
 
20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...
20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...
20141030 LinDA Workshop echallenges2014 - Open data commons for european citi...
 
How we lose etu hadoop competition
How we lose etu hadoop competitionHow we lose etu hadoop competition
How we lose etu hadoop competition
 

Grails

  • 1.
  • 3. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 • Groovy – 2003 (1.0 2007) – Dynamic Language (Ruby, Python) (Expressive, Ceremony, syntactic sugar) – JVM – JStack, JPM, javap, jconsole • Grails – Web Framework Page 3 Groovy & Grails - History
  • 4. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 4 History
  • 5. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 5 Start with Groovy …
  • 6. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 6 Start with Groovy (cont.)
  • 7. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 7 Start with Groovy (cont.)
  • 8. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 8 Start with Groovy (cont.)
  • 9. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 String country = country.name ?: “Unknown Country” String location = map ?.getLocation()?.getCoordinates() [‘Denver’, ‘Paris’, ‘Nice’, ‘Lima’]*.toUpperCase() // sendRequest(QkrUser.&create) … sendRequest(MethodClosure method) { … method(“new user”) Page 9 Fancy operators
  • 10. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 10 Express a regular expression
  • 11. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 [[sku:12, name:’walmart’], [sku:78, name:’walmart’], [sku:54, name:’walmart’], [sku:88, name:’saks’],[sku:33, name:’saks’]]  [walmart: [12, 78, 54], saks: [88, 33]] Map productSkusPerMerchant = skuMerchantPairs.groupBy{it.merchantName}.collectEntries{k, v -> [(k): v*.sku]} Page 11 Very fancy collection stuff
  • 12. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 12 Fancy other stuff
  • 13. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 13 Fancy String Stuff
  • 14. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 String message = “Dublin is better than most places” println “First Character: ${message[0]}” // D println “Last character: ${message[-1]}” //s // sub-string println message[0..5] // Dublin println message[5..0] // nilbuD Page 14 More fancy stuff
  • 15. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 def “Test findPositives” () { given: “A bunch of people” def service = new NumberService() when: “I pass in a list of numbers” def result = service.findPositives(numbers) then: “Only positive numbers returned” result == expected where: number | expected null | [] [-1, 4, 5] | [4, 5] Page 15 Really good testing opportunities – even for Java projects
  • 16. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 16 Closures
  • 17. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 17 Closure - JavaScript
  • 18. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 18 Closures
  • 19. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 19 Closures – Execute Around Pattern
  • 20. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 20 Execute and Around Pattern cont.
  • 21. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 21 Meta Programming
  • 22. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 • @Immutable • @TypeChecked • @GrailsTypeChecked • @CompileStatic - slow groovy code. • @Delegate Page 22 AST Transformations
  • 23. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 23 @Delegate
  • 24. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 24 AST Transformation write your own
  • 25. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 25 AST Transaction, write your own
  • 26. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 26 CartItem with Updated annotation
  • 27. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 27 Great JSON / XML support
  • 28. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 28 Great XML support cont.
  • 29. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 29 JSON Support
  • 30. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 trades = { buy 500 of ryanair sell 300 of microsoft buy 100 of google } trades = { buy(500).of(ryanair) sell(300).of(microsoft) buy(100).of(google) } Page 30 DSL
  • 31. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 - Rapid Web Application Dev Framwork - First Class Java Integration - Inspired by Ruby on Rails, Django - Convention over configuration - Leverages Java technologies Spring, Hibernate - Spring MVC under the hood - Service container - Full stack - Build in DB Page 31 Grails
  • 32. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 • grails –version • grails create-app MyApp • grails run-app • grails console – Very useful for understanding GORM, consequences etc. Page 32 Grails command line
  • 33. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 33 Grails – Convention over Configuration
  • 34. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 34 Grails Configuration – really good
  • 35. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 35 Grails - BuildConfig
  • 36. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 36 Grails - Plugins
  • 37. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 • Hibernate, Mongo, Redis etc • Put in the domain package and you get 20 instance methods and over 70 static methods • Instance: delete(), save(), validate(), refresh() • Static: count(), find(), findAll(), first(), last() – Note methods such as findByNameI) etc are synthesised code • Build in constraints: creditCard, email, unique (executes a query string during validation), Page 37 Grails - GORM
  • 38. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 Page 38 GORM Example
  • 39. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 • How good is your schema? • Saves can silently fail (failOnError) • Saves happen in the “Hession”. Hibernate decides when they get fired to DB and in what order (flush is false by default) • Changes to objects attached to the session persist, without a call to save • Cascading saves / deletes can take a bit of time to get right. • N + 1 problem • Just enable hibernate logging Page 39 GORM Gotcha’s
  • 40. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 • Perl, Python not on the JVM • Bash – remember, forget, can be difficult to read. • Anywhere the JVM is, you can run the script. • Define classes in your script • @Grab annotation to get dependencies from maven central • Run easily from command line A great use case for Grails / Groovy Page 40 Grails – Scripts
  • 41. ©2014 MasterCard. Proprietary and Confidential October 30, 2014 • Basics – any Java programmer. Experience of Python, JavaScript, even Scala etc. helps • Best practises – ShopThis! • Code reviews Page 41 How do we learn it?
  • 42. © Copyright – Internal Use Only

Editor's Notes

  1. Grails 2.4 – Java 8, Spring 4.0, Hibernate 4.2, @GrailsCompileStatic Groovy 2.3 – Traits, Grails 3.0 – Hadoop, Netty, Spring Boot, More support micro architectures, gradle,
  2. groupBy gives: [walmart: [[sku: 12, name: walmart], [sku:78,name: walmart], [sku:54, name:’walmart’]], saks: [[sku: 88, name: saks], [sku: 33, name: saks]]
  3. - Can use this approach for a Java project by adding a groovy and spock jar. Spock converts it to a Junit under the hood.
  4. Smalltalk
  5. Remember your audiences, traders or progammers Trades is a closure and if you can see the delegate of a closure. So you can set the closure of trades to something that implements buy, sell. Buy() and sell() can return something that implements of. But how does ryanair, microsoft etc get set to be a string?? The Delegate, like every Groovy object has a method called: Object getProperty(String name), implement it to just return name. But don’t forget error handling?
  6. All part of the build system Can pass arguments
  7. N + 1 problem: