$30 off During Our Annual Pro Sale. View Details »

Grails Interview Questions

Grails Interview Questions

Talk at Greach 2017

Sergio del Amo

March 31, 2017
Tweet

More Decks by Sergio del Amo

Other Decks in Programming

Transcript

  1. GRAILS INTERVIEW QUESTIONS
    objectcomputing.com/grails

    View Slide

  2. Sergio del Amo
    @sdelamo

    View Slide

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

    View Slide

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

    View Slide

  5. objectcomputing.com/grails
    BUILD TOOL

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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?

    View Slide

  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

    View Slide

  12. objectcomputing.com/grails
    Tell me about Grails
    environments

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  22. objectcomputing.com/grails
    GORM Implementations

    View Slide

  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?

    View Slide

  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.

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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?

    View Slide

  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

    View Slide

  35. objectcomputing.com/grails
    Constraints - Custom validator

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  46. grails-app/controllers/demo/BookController.groovy

    View Slide

  47. grails-app/conf/application.yml

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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?

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  68. YOU’RE HIRED

    View Slide

  69. ?

    View Slide

  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.

    View Slide