Grails Interview Questions

Grails Interview Questions

Talk at Greach 2017

30995fcc876f40073628b63ea5cfab59?s=128

Sergio del Amo

March 31, 2017
Tweet

Transcript

  1. GRAILS INTERVIEW QUESTIONS objectcomputing.com/grails

  2. Sergio del Amo @sdelamo

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

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

  5. objectcomputing.com/grails BUILD TOOL

  6. objectcomputing.com/grails Tell me about Grails Scaffolding

  7. objectcomputing.com/grails Scaffolding is a Grails feature that allows you to

    quickly generate CRUD interfaces for an existing domain
  8. 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
  9. 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
  10. 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?
  11. 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
  12. objectcomputing.com/grails Tell me about Grails environments

  13. objectcomputing.com/grails Grails supports the concepts of development environments by default

    and will configure itself accordingly when executed.
  14. 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
  15. 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
  16. objectcomputing.com/grails Execute Gradle tasks per environment ./gradlew -Dgrails.env=prod bootRun

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

    { systemProperties System.properties }
  18. objectcomputing.com/grails Do you know what the acronym ORM stands for?

  19. objectcomputing.com/grails Stands for Object-Relational Mapping. That it is to say:

    Mapping classes onto a relational database layer
  20. 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
  21. objectcomputing.com/grails Which GORM implementations do you know about?

  22. objectcomputing.com/grails GORM Implementations

  23. 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?
  24. 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.
  25. objectcomputing.com/grails Do you know what a derived property is?

  26. 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
  27. objectcomputing.com/grails What about using a transient instead the derived property?

  28. 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
  29. objectcomputing.com/grails In Grails applications you don’t see a lot of

    try / catch block. Why?
  30. objectcomputing.com/grails Groovy translates all exceptions into runtime exceptions, so Groovy

    code is never forced to catch an exception
  31. objectcomputing.com/grails Any area of Grails development where the distinction Checked

    and Unchecked exceptions is important?
  32. objectcomputing.com/grails Transactional methods get an automatic rollback if a method

    throws a runtime exception. Checked exceptions do NOT rollback transactions
  33. 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?
  34. 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
  35. objectcomputing.com/grails Constraints - Custom validator

  36. objectcomputing.com/grails Any other area of Grails where you can often

    use validators?
  37. objectcomputing.com/grails Yes, Command Objects and URLMappings

  38. objectcomputing.com/grails I wrote a test with for your validator. Do

    you see any problem with it?
  39. 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
  40. objectcomputing.com/grails I personally would use a where block in your

    Spock test.
  41. objectcomputing.com/grails In a Grails Controller, I have such an action:

    Do you see any problems?
  42. objectcomputing.com/grails Params object has a number of convenience methods for

    boolean, long, char, short …
  43. 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
  44. objectcomputing.com/grails However, the best approach would probably be to use

    a Command Object.
  45. objectcomputing.com/grails I want to display the last five books entered

    into the system. What do you think?
  46. grails-app/controllers/demo/BookController.groovy

  47. grails-app/conf/application.yml

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

  49. grails-app/views/book/latestBooks.gsp

  50. objectcomputing.com/grails Why are you getting the configuration values implementing GrailsConfigurationAware

    instead of using grailsApplication?
  51. 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
  52. objectcomputing.com/grails Could you write the service with a where query

    instead?
  53. grails-app/services/demo/BookService.groovy

  54. •Use DetachedCriteria •Compile time type checked •Compatible with static compilation

    •Use raw Groovy for criteria •Are compose-able WHERE QUERIES
  55. 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?
  56. objectcomputing.com/grails It is much easier! You should use Grails allowedMethods

    property in your controller!
  57. objectcomputing.com/grails Do you know how to create a multi-project build

    with Grails 3?
  58. 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
  59. objectcomputing.com/grails Can you tell me about Grails profiles?

  60. •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
  61. objectcomputing.com/grails What would you upgrade an app from Grails 3.2.6

    to 3.2.8?
  62. 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
  63. objectcomputing.com/grails How do you keep learning about Grails?

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

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

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

    Grails?
  67. objectcomputing.com/grails OCI is HOME TO GRAILS

  68. YOU’RE HIRED

  69. ?

  70. 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.