Given that I have a model denoting a city which holds a collection of streets.
public class City {
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Street> Streets { get; }
}
public class Street {
public int Id { get; }
public string Name { get; }
public IEnumerable<Building> Buildings { get; }
}
If a client is interested in all cities (api/cities/all) and I'd return him the full collection, this would lead to a massive response, depending on the size of the dataset. So I thought first about returning only street ids inside the streets collection. This however feels awkward because while the ids might be useful to further fetch the streets, they hold no meaningful value for a client (it doesn't make sense to populate a list of ids on a view in order to show what streets are in a city, doesn't it?).
My next idea was to ditch the streets collection completely and instead offer an API endpoint to fetch the streets of a city:
api/cities/3737/streets
That way I can fetch a complete list of streets, however the returned data then doesn't contain any information to where the streets belong to. If I a client now wants to show both the streets and the city, he'd have to make 2 API calls to get the information he needs.
What is a common way to return such data?
Aucun commentaire:
Enregistrer un commentaire