views:

467

answers:

1

I have a domain object in Grails that needs to store a set of key/value pairs for each instance. There should never be more then about 10 pairs. The users of the application have to be able to edit these key/value pairs. Right now I'm looking at storing the data in a HashMap for each instance of the domain class. While I think this will work it means I will have to write a fair amount of custom code for editing, updating, and showing these objects instead of using the code generated with grails generate-all. Is there a better way to store and edit the key/value pairs or should I just stick with HashMap?

+1  A: 

what if you modeled each key/value pair as first class objects? e.g. :

class MyKeyValue {
   static mapping={
      id generator:'assigned'
   }
   String id
   String value //i guess this could be an object of some sort, as long as it is a valid property (like Date)

   def getKey = {
      return id;
   }
}

class User {
    //...other properties
    //you'd put this property where the hashmap was originally.
    List MyKeyValue   
}

The generate-all command will create a rather plain UI for this, but its usable, and is a starting point for customization. You could then write your own constraints, and leverage the GORM's built in validation etc.

However, its a bit hacky imo - and the performance won't be great, but if you only have a few, i dont think thats gonna matter.

Chii