简体   繁体   中英

Is it OK for an API endpoint to return multiple models (e.g when configuring create/update forms in the front end)?

I am switching from normal CRUD resource methods (which often return multiple models required to populate a form) to an API scheme. My question is whether the normal create method I use should be made into a single API endpoint which returns multiple models, or whether each form element that requires data from the DB should have it's own endpoint and multiple HTTP requests should be made?

This is my first attempt at decoupling the front and back ends via an API, and all tutorials and examples I can find are so basic that they do not make this clear. I know how I would achieve either of these approaches, but don't know the recommended way. I'm looking for some idea of what might be considered best practice for this scenario as well as any potential issues that one or the other approach might cause me.

Since your goal is decupling, I would suggest to take into consideration creating Data Transfer Objects DTO) Pros and Cons of DTO

And specifically for your question, creating a DTO object encapsulating the models required from the front end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM