So I have a, let's say, Pet
abstract class, and three concrete implementations of it -- let's say Cat
, Dog
, and Bird
. As concrete classes do, these guys share common fields and also have unique fields -- e.g., they all have Legs
, but Birds alone have Wings
. So, the GSPs are different, the update methodology is different, etc.
The trick is that I need to be able to instantiate, validate, and persist an indeterminate number of instances in a single action. A straightforward implementation is essentially as follows:
create.gsp
Cat Hair Color: <g:textField name="catInstance.hairColor"/>
Dog Hair Color: <g:textField name="dogInstance.hairColor"/>
Bird Feather Color: <g:textField name="birdInstance.featherColor"/>
PetCommand.groovy
class PetCommand {
Cat catInstance
Dog dogInstance
Bird birdInstance
}
PetController.groovy
def save(PetCommand cmd) {
def catInstance = cmd.catInstance
def dogInstance = cmd.dogInstance
def birdInstance = cmd.birdInstance
/* do stuff */
}
Of course in a real application this gets significantly messier, and this completely defeats the purpose of using abstract classes.
Is there some way instead to bind multiple Pet
instances in a single fell swoop and then just loop through them and e.g., pass in parameters to update? I don't know, this whole thing is very confusing.
Command objects are not strictly necessary, but they fix a lot of the annoying redundancy of Grails controllers.