Implement CRUD operations of an MVC application based on a REST architecture
As I understand, a RESTful API aims to provide a communication layer for building on top of an application. Could the core application be build using the same REST logic?
So for example, build the REST architecture, and then implement all the database operations (CRUD) based on REST.
Therefore in an MVC application we have some controllers for implementing the REST service, like:
function Users(user_id){
\\ return user with id = user_id
}
function Articles (article_id){
\\ return article with id = article_id
}
And some controllers for handling the presentation. So if for example we want to list some articles in the homepage:
function homepage (){
for ($i=0; $i<10; $i++)
{
\\ request 'http://www.example.com/articles/' . $some_article_id;
}
\\ output to view
}
Not sure if the above makes sense, but the main point is that we add another layer in the MVC architecture, so that the presentation-logic Controllers no longer communicate directly with the models, but only through the REST Controllers:
In an MVC approach the data flow is:
presentation Controller ----> Model ----> presentation Controller ----> View
In an MVC - REST approach the data flow would be:
pres开发者_运维问答entation Controller ----> REST Controller ----> Model ----> REST Controller ----> presentation Controller ----> View
Do you see any disadvantages in the above approach? I believe that it would simplify coding but I searched a lot and haven't found any further info.
Yes there are many disadvantages.
- HTTP is intended for transfer of large grain resources.
- Implementing database transactions across HTTP requests is problematic, to say the least.
- You are using HTTP where is really isn't necessary and potentially introducing performance issues
and architecturally it is just the wrong place to use REST. See this article for a more in-depth discussion of why you should not attempt to expose your domain objects using REST.
精彩评论