Getting your Grails on: Grails Basics

 

This is an excerpt from my upcoming book Getting your Grails on, based on my presentation from Kansas City Developers Conference.

Grails is an open source full-stack MVC framework for the Java platform.  What do we mean by full-stack? Well, it practically does everything for you. It can even create database tables too. I know there are control freaks out there that have to understand everything. I can appreciate that, but hey we don’t use assembler anymore either.

Under the hood

Grails uses a few different components to help speed your development. It uses Spring to create the core controller for Grails. Hibernate is used for building data models and communicating with databases. SiteMesh is a templating framework that is used for your views. Grails currently in version 3.0.4. and you can download it here. This diagram helps understand where the pieces fit together.

Grails-Architecture

As a model view controller framework, each of these components is already in place. No need to wire up everything and configure your resources. You give up some flexibility to gain some time to completion with Grails, you don’t have to spend time in XML or other configuration files.

Grails leverages the Groovy language for it’s easy to use syntax and powerful features. It comes with tomcat to get you up and running quick, then you can switch it out later if needed. After years of Java development, I am amazed how easy it can be to put together a project and how it pulls in your dependencies.

Convention Over Configuration

Grails uses convention over configuration (also know as coding by convention) is a software design concept that decreases the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility.

The whole Grails architecture uses this to ensure full-stack consistency (example coming up). The common usage of names helps ensure intuitive code associations. Grails makes it easier for you to get started and doesn’t rely on mapping files like you find in Struts or Spring.

Model View Controller

Grails is a Model View Controller or MVC Framework. There seem to be a lot of different MVC Frameworks in use, perhaps you have used a few. Model–view–controller (MVC) is a software pattern for implementing user interfaces. It divides a given software application into three interconnected parts. The model in grails that represents the data and notifies its associated views and controllers when there is a change is called GORM. GORM stands for Grails Object Relational Mapper and is using Hibernate to make this happen.

The view for the front end that requests information from the model that it uses to generate an output to the user. This is using SiteMesh a layout and decorating framework, uses the decorator pattern. The controller is the interaction manager, it send commands to the model to update state, can also send commands to its associated views. Grails uses the Spring Framework to handle these interactions.

Grails Object Relational Mapper

GORM uses hibernate to perform CRUD functions with domain classes against the database, any inconsistency won’t start(Datasource.groovy). Domain classes are core to any application. They hold state about processes and hopefully also implement behavior. They are linked together through relationships; one-to-one, one-to-many, or many-to-many.

Here is an example Domain class:

package org.example
class Book {
String title
String author

static constraints = {
        title(blank: false)
       author(blank: false)
    }
}

This class will create a table called book with two fields title and author. The constraints section make sure that these fields cannot be empty.

GORM is Grails’ object relational mapping (ORM) implementation. Under the hood it uses Hibernate, a very popular and flexible open source ORM solution and thanks to the dynamic nature of Groovy with its static and dynamic typing, along with the convention of Grails, there is far less configuration involved in creating Grails domain classes. Finders can be used find by members of the domain class.  This was a real “Aha” moment for me as the code is not there. This when you see some real “Groovy Magic.”

Grails can do a lot for you if we want to override anything we just need to dig into the configuration files and change your settings. Groovy and Grails make a strong pair that can help you create applications quickly. In our next chapter, we are going to get into Grails more and demonstrate its strengths.

No comments yet.

Leave a Reply

Powered by WordPress. Designed by Woo Themes