1

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.

NaNuk
  • 141
  • 9

1 Answers1

0

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.

fotis
  • 1
  • 2
  • Thanks for the article. I took the time to read it. I'm not sure I'm convinced that I have a use case that really calls for DTO's. It seems that this pattern fits best where there are "virtual objects" that comprise a large collection of data from a variety of models. I am more referring to the use case where I have a form that I need to populate with data from several referenced entities for example, country, currency, phone code, etc. Thanks for taking the time to answer and please correct me if you think I am mistaken. – NaNuk Jun 01 '21 at 14:40
  • The way we are using it at my company (and I take from the linked article) can be summed up as : "A DTO is nothing more than a container class that exposes properties but no methods. A DTO is helpful whenever you need to group values in ad hoc structures for passing data around." and "A layer of DTOs isolates the domain model from the presentation, resulting in both loose coupling and optimized data transfer." – fotis Jun 02 '21 at 10:00
  • I take your point. DTO's may well be appropriate for this use case. I think I will be doing a little research and testing with this. Not sure if I will use in the way I was asking about, but I have other places in standard Laravel applications where I think DTOs would be appropriate for creating looser coupling than I have normally implemented. Thanks again for responding it has been useful. – NaNuk Jun 02 '21 at 12:16