add user define properties to a domain class

久未见 提交于 2019-12-12 14:23:35

问题


i have a requirement to allow the user to define some custom field in one of the system entities. do you have any suggestion/pattern/plugin that will help me add this feature to my application.

thanks,

Meni


回答1:


You can add a Map property to your domain class and store arbitrary data there. It's rather limited though. It will generate a table with varchar(255) keys and values, so you need to manage any type conversions yourself, e.g.

class Thing {
   String name
   Map extraProperties = [:]
}

int age = 123
def thing = new Thing(name: 'whatever')
thing.extraProperties.age = age.toString()
thing.save()

...

def thing = Thing.get(thingId)
int age = thing.extraProperties.age.toInteger()

See section "5.2.4 Sets, Lists and Maps" at http://grails.org/doc/latest/ for the brief online docs.




回答2:


Sounds like you want your application to be an infinitely adjustable wrench that users can modify at will. Is that fair?

I don't think it's possible or desirable. Think about what happens when you add an attribute to an existing domain object in Grails. The attribute is added to the ORM mapping, which means the tables have to be modified. The UI has another text box added for data entry; the list page has another column added to its table.

There's a lot going on when you add an attribute. How will you manage multiple users modifying the app all at the same time? What happens when one user is modifying a table while another is accessing the old version?

You ask too much. I don't think it's a reasonable requirement. Grails' sweet spot is rapid development of web-based CRUD applications. I don't think that includes modification by users at runtime.



来源:https://stackoverflow.com/questions/3382494/add-user-define-properties-to-a-domain-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!