✅ [.NET Minimal API's] How to design expansion of data through my API?
Hi guys,
I am designing a small toy project, basically a flash card application where a user can create sets of flash cards.
I have the following models:
I have a (more or less) CRUD repository for each model and a single
FlashCardService
of which both repositories are injected into.
This works fine.
Let's say I have an API endpoint that gets a specific flash card set from its Id
, which will return the model above.
where the service results is mapped to
Now, If a consumer wants to retrieve the data for each flash card, they would have to iterate through the list of card id's and then call a Get API endpoint for each card id.
I would argue that this is a bad experience for the consumer. A lot of work to retrieve some simple data.
Now, this is where my question arises. What is a proper and idiomatic way of being able to expand the API response with actual flash card data instead of just the ID's?
The only (and perhaps obvious) way I have thought about is simply to add a new model, like so
and then add a new method in the FlashCardSetService
that basically does the iteration I talked about before, which can then be used in the same endpoint, like so:
This way, I would say it is a nice experience for the user, as all they would have to do, is the add the query parameter to expand the ids to actual card data.
What do you guys think? Is there a more clean way to achieve this?
I hope it makes sense, thanks.4 Replies
I'd use another endpoint instead of a query param
Any particular reason why you prefer this approach?
I suppose using another endpoint makes it even more clear what each endpoint is responsible for and thus is a nice separation.
Yes, clarity and separation of concerns
If you then want to add, say, a view with paginated cards, or with only card previews, or whatever else, you just add a new endpoint
Not another
&previewOnly=true
Those are some good points. Thanks 🙂