设计问题:如何创建由多个领域对象组成的宁静界面?
因此,我们从公司的 grails 开始,创建这个使用 ExtJS 作为前端、Grails 作为后端的应用程序。
由于 ExtJS 专门使用 JSON 来填充数据表等,因此我们面临着创建这些接口的挑战,这些接口将以平稳的方式向 ExtJS 发送和接收 JSON 对象。
在对我们的领域模型进行深思熟虑后,我们最终选定了一个。很快,我们意识到系统中的某些表将由来自多个域对象的信息组成。
经过一番搜索后,我们发现了 JSON RESTful API for GORM 插件,它基本上公开了一个 RESTful 接口对于任何给定的域对象。
这就引出了问题的要点。
在我看来,有两种方法可以继续。
1) 为 ExtJS 中的每个数据表创建一个特定的控制器,该控制器将具有不同的操作,并且在每个操作中它将创建或接收 JSon 对象,以对数据表使用的不同域对象执行 CRUD。
在我看来,这会更耗时。无论如何,这是一个选择。
2)另一个选项是通过创建一个特定于我们想要填充的数据表的“复合”域对象来实现上述插件(该对象中的每个属性都是数据表中的一列)。之后,只需使用 GORM 插件的 JSON RESTful API 公开它,该插件基本上为我们处理了整个 CRUD 操作。
有没有一种方法可以创建一个像我们在域模型中想要的对象一样的对象,而无需在后端实际创建更多数据库表?
我相信我们走在正确的道路上,我们只需要专家的一点指导:)
提前致谢!
So, we're starting with grails in our company and we're creating this application that uses ExtJS as the frontend and Grails in the backend.
Because ExtJS works exclusively with JSON for populating datatables and such, we're presented with the challenge of creating these interfaces that will send and receive JSON objects to ExtJS in a restful manner.
After deliberating on our domain model, we settled on one. Quickly, we realized that some of the tables in the system will be composed of the information from more than one domain object.
After some searching around we found the JSON RESTful API for GORM plugin which basically exposes a RESTful interface for any given domain object.
This brings us to the main point of the question.
The way I see it, there are two ways to proceed.
1) Create a controller specific for each data table in ExtJS that will have the different actions and within each action it will create or receive the JSon object to do the CRUD on the different domain objects the data table uses.
This would be more time consuming, in my view. It's an option anyway.
2) The other option is to the the aforementioned plugin by creating a "composite" domain object specific for the datatable we want to populate (each attribute in this object is a column in the datatable). After this, just expose it using the JSON RESTful API for GORM plugin which basically takes care of the whole CRUD actions for us.
Is there even a way to create an object like the one we want in our domain model without actually creating more database tables in the backend?
I believe we're on the right path, we just need a little bit of guidance from experts :)
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的。
我最终使用了不同的方法,请告诉我您的想法。
基本上,我们最终使用了这个插件: http://grails.org/plugin/json-rest -api 公开域类并处理所有 CRUD 操作。
我们所做的是为每个类创建一个自定义 JSON 编组器,如下所示。
Bootstrap.groovy
Person.groovy
编组器实际上作用于调用“as JSON”方法时类将如何转换。
现在,由于有了该插件,我们可以执行
GET http://mydomain.com/api/person< /a>
我们得到一个 json 对象,其中包含来自 person 和其他类的信息,如下所示:唯一的事情是我们现在必须弄清楚如何通过更新或创建来更新街道地址(如果是)可能的。
想法?
Interesting.
I ended up using a different approach, let me know what you think.
Basically, we did end up using this plugin: http://grails.org/plugin/json-rest-api to expose a domain class and have all the CRUD operations taken care of.
What we did, was create a custom JSON marshaller for each class, like so.
Bootstrap.groovy
Person.groovy
The marshaller actually acts on how the class will be converted when the "as JSON" method is called.
So now, because of the plugin, we can do
GET http://mydomain.com/api/person
and we get a json object that has info from person and other classes like this:The only thing is that we now have to figure out how to update the street address from an update or a create, if it's possible.
Thoughts?
对于您的情况,我建议创建一组命令对象和处理它们的控制器。这将优雅地允许您的“复合”方法。
命令对象 是一个类,它具有域对象所具有的所有很酷的验证和数据绑定功能,但没有直接的持久性。
查看链接:
命令对象
JSON 响应
In your case, I would recommend creating a set of Command objects and Controllers which handle them. This will elegantly allow for your 'composite' approach.
A Command object is a class which has all the cool validation and data binding capabilities that Domain objects have, but without the direct persistence.
Links to check out:
Command Objects
JSON Responses