1

I am new to grails. One thing I have noticed in the codebase of my current project is that the domain classes and the controller classes share the same package. So, you have something like this:

grails-app/controllers/foo/BarController.groovy
grails-app/domain/foo/Bar.groovy

So, is this a common practice? What are pros and cons of this? Thank you very much.

JBT
  • 8,498
  • 18
  • 65
  • 104

2 Answers2

4

I think it's ok to use the same package for domain objects and controllers.

There is a practice called Package by Feature, which argues that grouping classes by what kind of component they are or what layer they are in is not as effective as grouping things together that contribute toward implementing the same functionality. When I work on projects packaged by layer I do a lot of hunting around going back and forth, grouping by feature would reduce that.

Usually domain objects have very little private about them. Also privacy in Groovy classes is nonexistent anyway.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
3

This is how "convention" works over "configuration" in Grails. This is a common practice. I haven't found a demerit using it this way.

Normally, when you create-domain-class or create-controller even the tests are added in the same package as the domain class/controller respectively.

Best example of convention I can cite is when you use

grails generate-all yourPackage.Domain

Stumbled upon a similar post related to Grails where exactly the package by feature aspect as mentioned by Nathan is explained. Hope that helps.

Community
  • 1
  • 1
dmahapatro
  • 49,365
  • 7
  • 88
  • 117