将用户定义属性添加到域类

发布于 2024-09-12 17:23:53 字数 88 浏览 7 评论 0原文

我需要允许用户在系统实体之一中定义一些自定义字段。您有任何建议/模式/插件可以帮助我将此功能添加到我的应用程序中吗?

谢谢,

梅尼

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-09-19 17:23:53

您可以将 Map 属性添加到域类中并在其中存储任意数据。但它相当有限。它将生成一个包含 varchar(255) 键和值的表,因此您需要自己管理任何类型转换,例如

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()

请参阅 http://grails.org/doc/latest/ 获取简短的在线文档。

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.

若能看破又如何 2024-09-19 17:23:53

听起来你希望你的应用程序是一个无限可调的扳手,用户可以随意修改。这公平吗?

我认为这是不可能或不可取的。想一想当您向 Grails 中的现有域对象添加属性时会发生什么。该属性被添加到 ORM 映射中,这意味着必须修改表。 UI 添加了另一个文本框用于数据输入;列表页的表格中添加了另一列。

添加属性时会发生很多事情。您将如何管理同时修改应用程序的多个用户?当一个用户正在修改表而另一个用户正在访问旧版本时会发生什么?

你问得太多了。我认为这不是一个合理的要求。 Grails 的最佳优势是基于 Web 的 CRUD 应用程序的快速开发。我认为这不包括用户在运行时的修改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文