Q1. What are the best practices to be followed while developing a grails application?
Building an application using grails framework is quite easy. But, when building it the right way we need to follow the best practices. Some of the practices followed while building a grails application are listed below:
- Controller- the controller logic should be as simple as possible. Duplication of code should be avoided.
- Service- it is the right choice for a coarse-grained code. By default, services are transactional in nature.
- Views- views should be as simple as possible. A consistent look should be maintained throughout the pages of the application.
Internationalization- all the text messages in view should be moved to “messages.properties”.
- Domain- the specific logic of the model domain should be placed in its own domain.
Q2. What do you mean by scaffolding? How do we use it?
Scaffolding is used to generate some basic CRUD interfaces for a domain class. It includes the necessary views and some controller actions for creating, updating, reading, and deleting CRUD operations. There are basically two types of scaffolding-
- Static scaffolding- with the help of this, controller and views can be generated to create certain interfaces using the command line.
- Dynamic scaffolding- using dynamic scaffolding, various functions like- index, show, edit, etc. can be dynamically implemented by default by using the runtime scaffolding mechanism.
For expressing a dependency on the scaffolding plugin, we can use-“build.gradle”.
Q3. What is the use of bootstrap.groovy?
Sometimes a situation occurs in which a particular task is to be carried out every time the application starts or stops.”bootstrap.groovy” is used for this particular situation. It is a simple plain groovy script which has two important closures: the “init” and “destroy” files. So, whenever the application starts or stops, these closures are called upon respectively. This “bootstrap.groovy” is present inside the grails-app/conf folder. It is used to define a code whenever the application performs any function.
Q4. What do you mean by closures?
A closure is a short and anonymous block of code which is just used to span a few lines of code. This block of code can even be entered as a parameter in the method. Closures are anonymous in nature. Formal parameters can also be contained in closures so that it can be more useful just like methods in groovy. Closures can also refer to a variable at the time when a closure is defined. Closures can be a type of parameter for a method. Few methods provided by closure itself are listed below:
- any() and every()
- Collect (), etc.
Q5. What do you understand by metaprogramming?
Metaprogramming is the ability of adding new methods or variables to the classes dynamically at run time. You can add methods or variables whenever and wherever you want. This is a very powerful ability. Be it the use of code production, unit tests, or anything in between, these capabilities increases the curiosity of the java developers. These are the programs that write or manipulate other programs. These are responsible for dynamic string execution. These help in exposing the internal of runtime engine to the programming codes through API’s. the meta object protocols make the program semantics explicit and extensible.