Issue Details (XML | Word | Printable)

Key: GRAILS-1824
Type: Improvement Improvement
Status: Open Open
Priority: Major Major
Assignee: Graeme Rocher
Reporter: Brad Whitaker
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Grails

Document how to add function calls and closures to Domain Classes

Created: 12/Nov/07 05:16 PM   Updated: 16/Nov/07 11:02 AM
Component/s: Documentation
Affects Version/s: 1.0
Fix Version/s: None

Time Tracking:
Not Specified


 Description  « Hide
None of the existing documentation sources (DGG book, online User Guide, reference doc) contain any mention, instructions, or examples for adding behavior to domain classes using either closures or methods. This is a barrier to learning Grails, particularly for people who believe in domain driven design.

(I'm raising this issue here per Graeme's Nabble post today stating 'If you have particular areas you want documented please raise JIRA issues'.)



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Graeme Rocher added a comment - 15/Nov/07 11:41 AM
This is covered under the plugins section:

http://www.grails.org/doc/RC1/guide/single.html#12.7%20Adding%20Dynamic%20Methods%20at%20Runtime

What is that you feel is missing from this?


Brad Whitaker added a comment - 15/Nov/07 01:19 PM
I'm suggesting something much more basic: documenting that a domain class may be given behavior by including closures and functions within the domain class definition (i.e. within MyDomainClass.groovy). Perhaps this has been undocumented because it seems obvious and trivial, but I would argue that it is not necessarily obvious to users new to Grails, plus there are various issues that are not obvious to a user even after modest experience with Grails.
  • Adding simple and obvious examples of a closure and a function such as these would be helpful:

def doSomethingClosureWithReturn = { arg ->
println "classic closure - doSomethingClosureWithReturn - arg=$arg"
return 3
}

int doSomethingElse(String arg) {
println "classic method call: arg=$arg"
return 4
}

  • there is also another syntax that works (although I don't understand why, and I don't understand if it is intentional and recommended):

def doSomethingAnotherWayWithArg(String arg) {
println "doSomethingAnotherWayWithArg: arg=$arg"
return 7
}

  • documentation of what keywords can be used to modify closures and methods: it seems that 'private' can be included in a method definition but it has no effect – the method can be called from anywhere; it seems that 'static' can be included and modifies the method properly.
  • how (or if) methods and closures impact scaffolding. (It seems that adding a closure to a domain class causes a useless 'entry' to be added to the generated create/edit/show views, but perhaps this shouldn't be documented but instead raised as an issue against scaffolding.)

Graeme Rocher added a comment - 16/Nov/07 07:47 AM
I understand your points, but all of this seems to relate to Groovy's documentation and not Grails'. Clearly Groovy needs to go down the same road in terms of the quality we have with Grails at the moment, but all of your examples are not really about Grails, but about Groovy.

Brad Whitaker added a comment - 16/Nov/07 11:02 AM
Ok, I understand. Perhaps I'm thinking more about a user guide than a reference guide. May the reference guide could include a simple statement along the lines of "methods and closures can be added to domain classes just as they can be added to any Groovy class". I don't think this simple fact is abundantly clear to someone new to Grails. (I also think that if there is another version of DGG it would be instructive to insert an example of a domain class with behavior – it's such a common thing.)