I have the following code:
const string endPoint = @"foo{0}?pageNum={1}&itemsPerPage={2}";
const int itemsPerPage = xxx;
InvoiceCollection response = await _apiClient
.GetAsync<InvoiceCollection>(string.Format(endPoint, _apiClient.OrgId, 1, itemsPerPage));
if (response?.TotalCount > itemsPerPage)
{
var allInvoices = (await DoPagination(endPoint,response.TotalCount, itemsPerPage))
.SelectMany(i => i.Invoices);
response.Invoices = (response.Invoices ?? Enumerable.Empty<Invoice>()).Concat(allInvoices);
}
return response;
Here the logic is to call an API, if the total number of results exceeds pre-defined itemsPerPage
, call all other pages and get the results in allInvoices
and combine it with the first response and return all the results together. Since IEnumerable.Concat
is used there will be a new object created and get assigned to response
.
I got a code review comment specifying "All responses sent down from the wire, to the HttpClient should be immutable." The comment is particularly about response
variable, which I disagree because its a locally scoped variable and does not affect the execution even if its mutable or immutable. I am not able to foresee any scenario where the code breaks because of that.
Is my argument valid?
Update:
Below is my InvoiceCollection
class:
public class InvoiceCollection
{
[JsonProperty("results")]
public IEnumerable<Invoice> Invoices { get; set; }
public int TotalCount { get; set; }
}
The response
is a bad naming which leads to confusion. Here response can be renamed as fisrtCollection
or something similar to that and it does not hold any information about API client.
async
method be eithervoid
orTask<...>
in C#?