Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. objectcomputing.com/grails Scaffolding is a Grails feature that allows you to

    quickly generate CRUD interfaces for an existing domain
  2. 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
  3. 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
  4. 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?
  5. 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
  6. 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
  7. 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
  8. 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?
  9. 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.
  10. 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
  11. 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
  12. objectcomputing.com/grails Transactional methods get an automatic rollback if a method

    throws a runtime exception. Checked exceptions do NOT rollback transactions
  13. 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?
  14. 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
  15. 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
  16. 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
  17. 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
  18. •Use DetachedCriteria •Compile time type checked •Compatible with static compilation

    •Use raw Groovy for criteria •Are compose-able WHERE QUERIES
  19. 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?
  20. 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
  21. •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
  22. 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
  23. ?

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