如何对提供基于 REST 的智能访问的存储库进行建模
我正在寻找一种独特的方式以 REST 方式查询以下用例:-
假设存储库包含以下内容:-
a. green color ball image of 1cm radius
b. yellow color ball image of 1cm radius
c. blue color ball image of 1cm radius
d. green color ball image of 2cm radius
e. yellow color ball image of 2cm radius
f. blue color ball image of 2cm radius
g. computer monitor icon image of size 32x32 pixels in png format
h. computer monitor icon image of size 64x64 pixels in png format
i. computer monitor icon image of size 32x32 pixels in ico format
j. computer monitor icon image of size 64x64 pixels in ico format
k. HR travel policy
l. HR new hire policy
g. HR promotion policy
1. Find all documents published after a certain date?
2. Find all documents published before a certain date?
3. Find all documents published between a certain set of dates?
4. Find all balls which are 1cm in radius
5. Find all documents whose download format is "png"
6. Find all documents whose size is 32x32 pixels
7. Find all balls which are green in color.
我们的存储存储库可以基于 Google Storage、Amazon S3、Mongodb GridFS、Java 内容存储库 (JCR 2.0)或简单的文件系统。
存储和检索上述数据的理想方式是什么?我希望 REST URL 尽可能具有表现力,以便我可以对上述任何用例进行建模 [1-6]。感谢有关如何设计通用存储库的任何指示,以便我可以使用适当的命名约定来根据上述查询获取文档。
I am looking for a unique way to query the following use cases in a REST fashion:-
Assuming the repository contains the following:-
a. green color ball image of 1cm radius
b. yellow color ball image of 1cm radius
c. blue color ball image of 1cm radius
d. green color ball image of 2cm radius
e. yellow color ball image of 2cm radius
f. blue color ball image of 2cm radius
g. computer monitor icon image of size 32x32 pixels in png format
h. computer monitor icon image of size 64x64 pixels in png format
i. computer monitor icon image of size 32x32 pixels in ico format
j. computer monitor icon image of size 64x64 pixels in ico format
k. HR travel policy
l. HR new hire policy
g. HR promotion policy
1. Find all documents published after a certain date?
2. Find all documents published before a certain date?
3. Find all documents published between a certain set of dates?
4. Find all balls which are 1cm in radius
5. Find all documents whose download format is "png"
6. Find all documents whose size is 32x32 pixels
7. Find all balls which are green in color.
Our storage repository could be based on Google Storage, Amazon S3, Mongodb GridFS, Java content repository (JCR 2.0) or a simple file system.
What would be the ideal way to store and retrieve the above data. I would like the REST URL to be as expressive as possible, so that I can model any of the above use cases [1-6]. Appreciate any pointers on how to design a generic repository, so that I can use appropriate naming conventions to fetch documents based on the above queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议在这里分开两个问题:1)建模存储库; 2) 设计 RESTful 访问。当开发刚刚开始时,这两个问题总是齐头并进。随着项目的发展,您可能需要为存储库设计聚合点或简单的不同观点。理解这些概念仅在表面上相似总是好的。
就您的 7 个问题而言,首先您需要确定“文档”的概念是否可以完全被“文档实例”的概念替换。例如,如果红球的 ID 为 1,那么以下两个资源是否有效?
您能否将新球发布到以下两个资源或仅一个资源?
您能通过单个或多个资源找到所有绿球吗?
回答这些问题很重要,这样您的 API 才会明确。
其次,您需要决定您希望资源看起来如何“SEO 友好”。例如:
如果您的 API 完全开放并用于驱动公共网站上的链接,那么这就是一个“问题”。我之所以将“SEO友好”和“关注”放在引号中,是因为SEO专家仍然不同意搜索引擎是否喜欢某种方式或引擎根本不关心。
从实现的角度来看,使用 URL 参数更容易、更快速且更具可扩展性。但如果您不期望出现大量的组合,那么这两种方法都同样有效。
根据个人经验,我还建议使用公共路径对搜索类型资源(与身份资源相对)进行分组。例如:
祝设计好运。谈到 REST,没有一种正确的方法。只要有意义——你就走在一条好的道路上。
I would recommend separating two concerns here: 1) modelling storage repository; and 2) designing RESTful access. When a development just starts, these two concerns always go hand in hand. As a project evolves, you might need to design aggregate points or simply different points of view for the repository. It's always good to understand that the concepts are similar only on a surface.
As far as your 7 questions go, first you need to decide if a concept of a "document" is fully replaceable by a concept of "document instance". For example, if a red ball has ID of 1 then are the following two resources valid?
Can you POST a new ball to the both of the following resources or just one?
Can you find all green balls by a single or multiple resources?
It's important to answer those questions so that your API is unambiguous.
Second, you need to decide how "SEO-friendly" you want your resource to look. For example:
This is a "concern" if your API is wide open and is used to drive links on a public web site. The reason why I put "SEO-friendly" and "concern" in quotes is because SEO experts still don't agree if search engines prefer one way or another or the engines don't care at all.
From the implementation point of view, it's easier, faster and more scalable to use URL parameters. But if you don't expect crazy amount of combinations then both approaches will work equally well.
From a personal experience I would also recommend to group search-type resources (as oppose to identity resources) with a common path. For example:
Good luck with a design. There is no one right way when it comes to REST. As long as it makes sense - you're on a good path.