Slide 1

Slide 1 text

GRAILS INTERVIEW QUESTIONS objectcomputing.com/grails

Slide 2

Slide 2 text

Sergio del Amo @sdelamo

Slide 3

Slide 3 text

objectcomputing.com/grails https://www.flickr.com/photos/42353480@N02/5768808772/sizes/l/ Welcome! Let's talk about your Grails experience

Slide 4

Slide 4 text

objectcomputing.com/grails https://www.flickr.com/photos/42353480@N02/5768808772/sizes/l/ Which build tool does Grails use?

Slide 5

Slide 5 text

objectcomputing.com/grails BUILD TOOL

Slide 6

Slide 6 text

objectcomputing.com/grails Tell me about Grails Scaffolding

Slide 7

Slide 7 text

objectcomputing.com/grails Scaffolding is a Grails feature that allows you to quickly generate CRUD interfaces for an existing domain

Slide 8

Slide 8 text

objectcomputing.com/grails DYNAMIC SCAFFOLDING dependencies { // ... compile "org.grails.plugins:scaffolding" // ... } build.gradle class Book { String name String about } grails-app/domain class BookController { static scaffold = Book } grails-app/controllers class BookController { static scaffold = true } Old Grails 2.x way

Slide 9

Slide 9 text

objectcomputing.com/grails STATIC SCAFFOLDING grails generate-controller Book grails generate-views Book grails generate-all Book Generate a controller and the views for a domain class

Slide 10

Slide 10 text

objectcomputing.com/grails Given this scaffolding snippet. Take a look at the use of DELETE. Did not browsers support only GET and POST for forms?

Slide 11

Slide 11 text

objectcomputing.com/grails Actually POST is used. DELETE is passed as a parameter named _method.Request gets intercepted by org.grails.web.filters.HiddenHttpMethodFilter.java which wraps the request and uses DELETE AWESOME! +1

Slide 12

Slide 12 text

objectcomputing.com/grails Tell me about Grails environments

Slide 13

Slide 13 text

objectcomputing.com/grails Grails supports the concepts of development environments by default and will configure itself accordingly when executed.

Slide 14

Slide 14 text

objectcomputing.com/grails ENVIRONMENTS environments: development: dataSource: dbCreate: create-drop url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE test: dataSource: dbCreate: update url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE production: dataSource: dbCreate: none url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE application.yml

Slide 15

Slide 15 text

objectcomputing.com/grails CONFIGURATION FILES PER ENVIRONMENT grails-app conf application-production.yml application-development.yml application-test.yml Checkout mrhaki blog post: Using External Configuration files per environment

Slide 16

Slide 16 text

objectcomputing.com/grails Execute Gradle tasks per environment ./gradlew -Dgrails.env=prod bootRun

Slide 17

Slide 17 text

objectcomputing.com/grails Pass System Properties bootRun { systemProperties System.properties } integrationTest { systemProperties System.properties }

Slide 18

Slide 18 text

objectcomputing.com/grails Do you know what the acronym ORM stands for?

Slide 19

Slide 19 text

objectcomputing.com/grails Stands for Object-Relational Mapping. That it is to say: Mapping classes onto a relational database layer

Slide 20

Slide 20 text

objectcomputing.com/grails In GRAILS we normally use GORM. A data access toolkit which provides a rich set of APIs for accessing relational and non-relational data

Slide 21

Slide 21 text

objectcomputing.com/grails Which GORM implementations do you know about?

Slide 22

Slide 22 text

objectcomputing.com/grails GORM Implementations

Slide 23

Slide 23 text

objectcomputing.com/grails In the previous scaffolding code I found this method: def index(Integer max) { params.max = Math.min(max ?: 10, 100) respond Book.list(params), model:[bookCount: Book.count()] } I have not defined list() or count() methods. What is going on?

Slide 24

Slide 24 text

objectcomputing.com/grails Those are Dynamic Finders. A dynamic finder looks like a static method invocation, but the methods themselves don’t actually exists in any form at the code level. Instead, a method is generated for us based on the properties of a given class.

Slide 25

Slide 25 text

objectcomputing.com/grails Do you know what a derived property is?

Slide 26

Slide 26 text

objectcomputing.com/grails A derived property is one that takes its value from a SQL expression, often but not necessarily based on the value of one or more persistent properties

Slide 27

Slide 27 text

objectcomputing.com/grails What about using a transient instead the derived property?

Slide 28

Slide 28 text

objectcomputing.com/grails With a derived property you have the ability to do queries such as: Product.findAllByTaxGreaterThan(21.12) The drawback of derived queries is that you may give some database portability

Slide 29

Slide 29 text

objectcomputing.com/grails In Grails applications you don’t see a lot of try / catch block. Why?

Slide 30

Slide 30 text

objectcomputing.com/grails Groovy translates all exceptions into runtime exceptions, so Groovy code is never forced to catch an exception

Slide 31

Slide 31 text

objectcomputing.com/grails Any area of Grails development where the distinction Checked and Unchecked exceptions is important?

Slide 32

Slide 32 text

objectcomputing.com/grails Transactional methods get an automatic rollback if a method throws a runtime exception. Checked exceptions do NOT rollback transactions

Slide 33

Slide 33 text

objectcomputing.com/grails I am developing a Grails application to organise my agenda. I have a domain class: class AgendaEvent { String eventName Date eventDate } I want to restrict the application to enter only upcoming dates. Could you help me?

Slide 34

Slide 34 text

objectcomputing.com/grails No problem! A Grails domain class can express constraints simply by defining a static property named constraints, that has a closure as a value

Slide 35

Slide 35 text

objectcomputing.com/grails Constraints - Custom validator

Slide 36

Slide 36 text

objectcomputing.com/grails Any other area of Grails where you can often use validators?

Slide 37

Slide 37 text

objectcomputing.com/grails Yes, Command Objects and URLMappings

Slide 38

Slide 38 text

objectcomputing.com/grails I wrote a test with for your validator. Do you see any problem with it?

Slide 39

Slide 39 text

objectcomputing.com/grails Replace validate() with no args with validate with a List of properties which are under test. Thus, you test eventDate on isolation without the need to provide valid values for other properties

Slide 40

Slide 40 text

objectcomputing.com/grails I personally would use a where block in your Spock test.

Slide 41

Slide 41 text

objectcomputing.com/grails In a Grails Controller, I have such an action: Do you see any problems?

Slide 42

Slide 42 text

objectcomputing.com/grails Params object has a number of convenience methods for boolean, long, char, short …

Slide 43

Slide 43 text

objectcomputing.com/grails You could use the parameters as action method arguments. If you provide wrong types default values will be binded. e.g. null, false

Slide 44

Slide 44 text

objectcomputing.com/grails However, the best approach would probably be to use a Command Object.

Slide 45

Slide 45 text

objectcomputing.com/grails I want to display the last five books entered into the system. What do you think?

Slide 46

Slide 46 text

grails-app/controllers/demo/BookController.groovy

Slide 47

Slide 47 text

grails-app/conf/application.yml

Slide 48

Slide 48 text

encapsulate your business logic in Grails services grails-app/services/demo/BookService.groovy

Slide 49

Slide 49 text

grails-app/views/book/latestBooks.gsp

Slide 50

Slide 50 text

objectcomputing.com/grails Why are you getting the configuration values implementing GrailsConfigurationAware instead of using grailsApplication?

Slide 51

Slide 51 text

objectcomputing.com/grails It is not ideal because the application is paying the performance price of retrieving the config value every time the service method is invoked. Services are singletons by default. void setConfiguration(Config co) will be called just once

Slide 52

Slide 52 text

objectcomputing.com/grails Could you write the service with a where query instead?

Slide 53

Slide 53 text

grails-app/services/demo/BookService.groovy

Slide 54

Slide 54 text

•Use DetachedCriteria •Compile time type checked •Compatible with static compilation •Use raw Groovy for criteria •Are compose-able WHERE QUERIES

Slide 55

Slide 55 text

objectcomputing.com/grails I want to restrict the invocations of a controller’s action to DELETE requests. I plan to create an interceptor and reject any request with a HTTP verb other than DELETE. What do you think?

Slide 56

Slide 56 text

objectcomputing.com/grails It is much easier! You should use Grails allowedMethods property in your controller!

Slide 57

Slide 57 text

objectcomputing.com/grails Do you know how to create a multi-project build with Grails 3?

Slide 58

Slide 58 text

objectcomputing.com/grails WHAT ABOUT MULTI-PROJECT dependencies { // ... compile project(“:myplugin”) // ... } myapi/build.gradle grails create-app myapi —profile=rest-api grails create-plugin myplugin --profile=plugin CREATE AN APP AND A PLUGIN include 'myapi', 'myplugin' settings.gradle

Slide 59

Slide 59 text

objectcomputing.com/grails Can you tell me about Grails profiles?

Slide 60

Slide 60 text

•angular - A profile for creating applications using AngularJS •rest-api - Profile for REST API applications •base - The base profile extended by other profiles •angular2 - A profile for creating Grails applications with Angular 2 •plugin - Profile for plugins designed to work across all profiles •profile - A profile for creating new Grails profiles •react - A profile for creating Grails applications with a React frontend •rest-api-plugin - Profile for REST API plugins •web - Profile for Web applications •web-jboss7 - A Profile for Creating a JBoss 7.1 EAP Project •web-plugin - Profile for Plugins designed for Web applications •webpack - A profile for creating applications with node-based frontends using webpack

Slide 61

Slide 61 text

objectcomputing.com/grails What would you upgrade an app from Grails 3.2.6 to 3.2.8?

Slide 62

Slide 62 text

objectcomputing.com/grails I would checkout the release notes in grails.org Normally it involves increasing a version number in gradle.properties I would diff two apps created from scratch with each version? Or use a https://github.com/erichelgeson/ grails-versions

Slide 63

Slide 63 text

objectcomputing.com/grails How do you keep learning about Grails?

Slide 64

Slide 64 text

objectcomputing.com/grails I checkout every Monday guides.grails.org

Slide 65

Slide 65 text

objectcomputing.com/grails I am subscribed to GroovyCalamari.com

Slide 66

Slide 66 text

objectcomputing.com/grails Do you know how to get commercial support for Grails?

Slide 67

Slide 67 text

objectcomputing.com/grails OCI is HOME TO GRAILS

Slide 68

Slide 68 text

YOU’RE HIRED

Slide 69

Slide 69 text

?

Slide 70

Slide 70 text

OCI 12140 Woodcrest Exec. Dr., Ste. 250 Saint Louis, MO 63141 USA © 2017, All Rights Reserved. No part of this publication may be photocopied or reproduced in any form without written permission from OCI. Nor shall the OCI logo or copyright information be removed from this publication. No part of this publication may be stored in a retrieval system, transmitted by any means, recorded or otherwise, without written permission from OCI. While every precaution has been taken in preparing this material, including research, development and testing, OCI assumes no responsibility for errors or omissions. No liability is assumed by OCI for any damages resulting from the use of this information.