6

我需要能够获取基于对象集合的集合。为此,我正在考虑几种方法,但不确定哪一种最有意义,所以我会同时发布它们。如果有更好的方法我可能没有考虑过,请随时发表您的建议:)

例如,我将使用产品和评论(产品评论)作为资源。

目标:我们希望获得所有产品的评论

方法一:

POST api/Products { json: filters, limits, etc } // returns api/Products/<id>
GET api/Products/<id>/Reviews { json: filters, limits, etc } // returns json array 
// ... of reviews present in products
// id in this case would refer to the collection, which would be cached

方法 B:

GET api/Reviews { json: filters } // returns json array of reviews, but needs to 
// ... have either all of the product ids passed as an array (not practical with 
// ... huge collections), or needs to have all of the api/Products filters passed, 
// ... in which case making the code unDRY

方法 C:

GET api/Reviews { json: filters: { products: 'api/Products?filters' }...
// is this reasonable?

提前感谢您的帮助,如果这个问题是菜鸟,我深表歉意,我还是 REST 新手

方法 D:

access individual products
GET api/Product/<id>

access collection of products:
POST api/Products?filters=stuff..
GET api/Products/<id>

access collection of products' reviews

POST api/Products/<id>/Reviews?filters=stuff
GET api/Products/<id>/Reviews/<id>

... this would thus allow us to cache the result easily, does this seem reasonable to you? 
4

1 回答 1

12

目标:我们希望获得所有产品的评论

基本上,GET /api/products/<id>应该返回 a product,并且GET /api/products/<id>/reviews应该返回reviews给定的all product

如果您想获取所有products内容,您将使用GET /api/products. 当您同时考虑reviewsproducts作为资源时,我建议您公开所有reviews

GET /api/reviews

但你可能想products 他们的reviews. 这意味着使用/api/products(例如一组过滤器)获取一些产品并使用reviews. 该expand术语来自 OData 协议:http ://www.odata.org/documentation/uri-conventions#ExpandSystemQueryOption 。

你可以这样做:

GET /api/products?$expand=reviews

GET /api/products?filter=foo&$expand=reviews

这个 URI 的意思是:“标识products每个.reviewsproduct

于 2012-08-28T07:06:28.277 回答