When implementing a RESTful web service with an MVC architecture with something basic like a CRUD, it is necessary to know that the specification indicates that the entity / model for which it was designed must be called in each of the controllers.
Based on the SOLID Principles, the first is The principle of single responsibility , which states:
Each module or class must have responsibility for only one part of the functionality provided by the software and this responsibility must be fully encapsulated by the class.
We can realize that by making the classes dependent, you would be generating a low cohesion and a high coupling. So when making a modification to one would directly affect the other class.
The main problem with this RESTful approach is that if you have two Models one called Post and another Comment indicating the specification to be able to call a Post with all The associated Comments would be as follows:
GET /post/1
{
"title": "My Post",
"author": {
"firstName": "Angel",
"lastName": "Oropeza"
},
"comments": [1, 2, 3, 4]
// ... more fields here
}
GET /comment/1
{
// ... more fields here
}
...
GET /comment/4
{
// ... more fields here
}
That is, you must make a call to get the Post with id 1 and then make a call to each of the comments associated with this Post.
Finally, mention that some pages have migrated their REST services to GraphQL as GitHub. You can find more information about Here .