[英]Writing an API, benefits of: including nested objects automatically, not at all, or provide a parameter to specify which to include?
For example, we have an entity called ServiceConfig
that contains a pointer
to a Service
and a Professional
. 例如,我们有一个称为
ServiceConfig
的实体,其中包含pointer
Service
和Professional
的pointer
。 If returned without including the fields would look like this: 如果返回但不包含字段,则将如下所示:
{
'type': '__Pointer',
'className': 'Service',
'objectId': 'q92he840'
}
At which point they could query again to retrieve that service. 此时,他们可以再次查询以检索该服务。 However, it is often the case that they need the
Service
name. 但是,通常情况下,他们需要
Service
名称。 In which case it is inefficient to have to query again to get the service every time. 在这种情况下,每次必须再次查询才能获得服务的效率很低。
Options: 选项:
Automatically return the Service
. 自动返回
Service
。 In which case, we should automatically return the Industry
for that Service
as well in case they need that... same applies to all. 在这种情况下,如果他们需要,我们也应该自动返回该
Service
的Industry
。。。 Seems like we're returning data too often here. 似乎我们在这里经常返回数据。
Allow them to pass an includes
parameter that specifies which entities to include. 允许他们传递一个
includes
参数,该参数指定要包含的实体。 Format is an array of strings where using a .
格式是使用的字符串数组
.
can allow them to include subclasses. 可以允许他们包括子类。 In this case
['Professional', 'Service.Industry']
would work. 在这种情况下,
['Professional', 'Service.Industry']
将起作用。
Can anyone identify why any one solution would be better than the others? 谁能确定为什么任何一种解决方案都会比其他更好? I feel that the last solution is the best, however it does not seem to be common to do to in the APIs I've seen.
我觉得最后一个解决方案是最好的,但是在我见过的API中似乎并不常见。
This is a good API Design decision to spend your time on before you release an initial version. 这是一个不错的API设计决策,需要花时间在发布初始版本上。 Both your approaches are valid and it all depends on what you think are the most common ways that clients would use your API.
这两种方法都是有效的,并且都取决于您认为客户端使用您的API的最常见方式。
Here are some points that you could consider: 您可以考虑以下几点:
If I had to make a choice and have a little bit more leeway in terms of effort, I would go with the 2nd option, even if it is a simple version at first. 如果我不得不做出选择并且在努力方面还有更多余地,那么我会选择第二个选项,即使起初它是一个简单的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.