卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32024

Why is aggregation function bad idea for RESTful?

As title: Why is aggregation function bad idea for RESTful? Although I know CRUD is good for RESTful.

For example, the resource is 'employee', and client needs to retrive sum of total 'salary' of all employees. Shouldn't RESTful service provide such sum function?

Further question: if aggregation function is bad for RESTful, how can a client get sum of total salary? To retrieve all 'employee' records and sum up itself?

Upvotes: 7

Views: 2494

Answers (1)

benjiman
benjiman

Reputation: 4078

I wouldn't say that exposing the results of operations (i.e. an aggregation function) as resources in REST has to be considered as bad.

From the 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.

It is quite common to treat a processing function as a resource, and use HTTP GET to fetch a representation containing the output of the processing function. You can also use query parameters to supply inputs to the processing function.

So why not provide the result of a salary aggregation of a number of employees as a resource, i.e. like this:

GET /employees/aggregation?data=salary

or more general:

GET /aggregator?resource=employee&data=salary

You could even filter the group of employees for which the salaries should be aggregated, i.e. like this:

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

Upvotes: 11

Related Questions