Jul 202013
 

Before going through your journey in learning Grails, it is important to understand the MVC Pattern. Most web application framework uses this architecture to help make clean and manageable projects. It is surprisingly simple and easy to learn too.

The Old Dirty Way

When I first worked in the 90’s, the Internet is just beginning to be popular. Information exchange between developers from different companies are seldom. Developers use techniques they developed on their own. The common result are badly written and hard to maintain software.

Back in year 2000 to 2001, I was part part of a team working on a big project. It was our standard practice that for a single JSP page, we do the following:

  • Handle list, create, edit for a specific table. We pass parameter to know which section of the JSP page needs to be rendered.
  • Get a database connection and hardcode all the SQL statements for select, insert, and update.
  • Render different forms based on the state of the page. E.g. Create and Edit Form.
  • Perform server side validations
  • Perform client side validations

If you are new to web development and don’t understand the list above, just think that I was implementing different functionalities inside a single file that is several thousand lines of code. Another analogy is writing a a program thousands of lines long but not broken into functions – all are just inside 1 very long function. It’s a nightmare maintaining such code!

Nowadays, developers are more aware of what’s happening in the industry. They share ideas of best practices and learn from mistakes of others. Projects now are much healthier than before.

Separation of Concern

The common theme (but not the only one) of most best practices now is to encourage separation of concerns. Imagine that instead of having a single program file that handles all logic of a program mentioned above, you break the code into several pieces. For example:

  • Customer.java – represents the data of a customer. E.g. first name, last name, birthday, gender, and marital status.
  • CustomerService.java – contains code on how to talk to database. E.g. insert a customer record.
  • list.jsp – contains html code on how to display a list of customer
  • create.jsp – contains html form for creating a customer
  • edit.jsp – contains html form for updating information about a specific customer
  • validations.js – contains javascript code for client side validations
  • Validations.java – contains code for server side validations
  • PersonServlet.java – handles different types of request and decides what to do.

Wouldn’t that be better and more manageable? That is indeed the goal for “separation of concerns” – that specific class or code handles only one purpose. This removes complexities and make your code-base simpler.

Model View Controller

MVC pattern is a software architecture that encourages separation of concerns, such that:

  • Model – handles data representation and operations. For example, Person and PersonService above are part of the Model. Your classes that represents entities (E.g. customer, product) and business logic/functionalities (e.g. create customer record), collectively represents the model.
  • View – handles how data is viewed by the user. For example, the HTML code for the list of customers.
  • Controller – handle the code that links a user to the system. It receives request or events from the user, prepare data and invoke proper business logic, and then return the view that represents the result of user actions.

Grails MVC Example

Here is a simple illustration in Grails:

  • Model

    Customer.groovy represents customer data

    class Customer {
        String firstName, lastName
        Date birthday
        String gender
        String maritalStatus
    }
    
  • View

    list.gsp handles HTML code to display a list of customers

    <table>
        <tr><th>Name</th><th>Gender</th></tr>
        <g:each in="${customerList}" var="cust">
            <tr><td>${cust.lastName}, ${cust.firstName}</td><td>${cust.gender}</td></tr>
        </g:each>
    </table>
    
  • Controller

    CustomerController.groovy handles web requests pertaining to customers

    class CustomerController {
        def list() {
            def list = Customer.list()
            [list:list]
        }
    }
    

    The controller is invoked whenever the URI /customer/list is invoked (event initiated by the user). What it does is invoke the business logic (model) using the command Customer.list(). It then invokes the view list.gsp automatically by virtue of convention. (I.e. the name of gsp invoked is the same as the controller method.)

This is just an introduction to MVC concepts. Details on how to harness it in Grails will be covered in future posts.


List of Tutorials for Beginners

Comments

comments