Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails Package Structure

I am picking up an existing codebase that is Groovy and Grails, but the package structure seems very odd to me.

For a domain class they put it in the following package com.company.domain then for the controller of that class it is com.company.controller

That structure seems very off to me since the domain and controller classes are already organized under their own folders in the grails-app folder.

My plan is to redo the packages and group based on actual use such as com.company.billing and com.company.util .

Are there any disadvantages to my plan? Is there anything good about the current package structure that I'm missing?

like image 811
Jeff Beck Avatar asked Mar 22 '11 13:03

Jeff Beck


People also ask

What is the Grails framework?

Technologies such as GSP, JSON Views, and Markup Views help developers effortlessly generate HTML, JSON, and XML. The Grails framework is an open source Apache 2 License project.

How do I create a grails app?

Creating a Grails app is about as simple as could be - once you have installed Grails, simply run the create-app command: If you don’t specify a package, the app name will be used as the default package for the application (e.g., myapp ).

How do I download a Grails project from the terminal?

You can even download your project from the command line using a HTTP tool like curl (see start.grails.org for API documentation): Creating a Grails app is about as simple as could be - once you have installed Grails, simply run the create-app command:

How do I use Grails'interactive mode?

You can also use Grails' Interactive Mode to run the Grails runtime, from which you can issue any Grails command without waiting for the runtime to spin up for each task. In this guide we will be preferring the Grails wrapper for most commands.


1 Answers

I think package names should separate code belonging to different business aspects. For e.g. a shopping website, I'd recommend:

  • com.mycompany.myfancywebsite.product to all product related stuff (e.g. Product domain class, ProductDetailController etc.)
  • com.mycompany.myfancywebsite.cart to all shopping cart related stuff (e.g. CartController, ShippingCostCalculationService etc.)
  • com.mycompany.myfancywebsite.payment to all payment related things

IMHO it does not make sense to use package names to distinguish the "type" of code (e.g. domain, controller, service...), this simply adds no value to it.

I'd also recommend carefully using util in package names, this might be a sign that your code is not focussed enought.

For further reading, see the excellent book "Clean code". Also see http://weblog.dangertree.net/2008/11/22/grails-package-naming/

like image 100
Stefan Armbruster Avatar answered Oct 04 '22 00:10

Stefan Armbruster