为什么聚合功能对RESTful来说不好?

标题:为什么聚合功能对RESTful不好?虽然我知道CRUD对RESTful有好处.

例如,资源是“员工”,客户需要检索所有员工的总“工资”总和. RESTful服务不应该提供这样的求和功能吗?

进一步的问题:如果聚合功能对RESTful不利,客户如何获得总薪水总和?要检索所有“员工”记录并总结一下?

解决方法

我不会说暴露操作结果(即聚合函数)作为REST中的资源必须被认为是坏的.

RESTful Webservices Cookbook(O’Reilly):

One of the most common perceptions of REST’s architectural constraints is that they
only apply to resources that are “things” or “entities” in the application domain. Although
this may be true in a number of cases,scenarios that involve processing functions
challenge that perception.

将处理函数视为资源是很常见的,并使用HTTP GET来获取包含处理函数输出的表示.您还可以使用查询参数为处理函数提供输入.

那么为什么不提供一些员工的工资汇总结果作为资源,例如:

GET /employees/aggregation?data=salary

或更一般:

GET /aggregator?resource=employee&data=salary

您甚至可以过滤应该汇总工资的员工组,例如:

GET /employees/aggregation?data=salary&divison=sales

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...