vtex-apps / reviews-and-ratings

5 stars 20 forks source link

ASP.NET Core usage style #65

Open igorgiovannini opened 3 years ago

igorgiovannini commented 3 years ago

In our enterprise we are currently evaluating VTEX.

Having a look at the source code of this extension, I noticed you handle manually various stuffs for a specific endpoint (see https://github.com/vtex-apps/reviews-and-ratings/blob/master/dotnet/Controllers/RoutesController.cs).

Is there a technical need to handle the various cases manually, instead of taking advantage of the facilities offered by the framework (i.e. using annotations)? I think for example to the query strings, or the payload of a POST request. You can expose the same endpoint, with different action methods and HTTP actions of course.

Thanks! :smile:

polishq commented 3 years ago

Hi @igorgiovannini , thanks for the question!

If I understand correctly, you want to know why we do not take advantage of annotations like [HttpPut], [HttpGet], and [HttpDelete] to handle different actions for the same endpoint. And also why we do not use [FromBody] to get the payload automatically parsed.

We do not use annotations to define the path a controller method supports because we use our dotnet/services.json file to define the routes. This is because before reaching our app, the requests will reach the VTEX IO Router, so both of them need to understand the same route definitions (otherwise we would need to synchronize the routes definitions, which is error-prone). The service.json route definition associates a controller method to a path. Having multiple controller methods for the same path would be possible only if we had the HTTP action to combine with the path in the route definition.

That said, we can use these annotations. The problem is that we need to use different endpoints for each action (one for a PUT, one for a GET, and another one for a DELETE, for instance). Having one controller method per action, you can also use [FromBody], [FromQuery], and cache annotations as you would do in any .Net app.

In this particular example, we want to be RESTful and have one endpoint to handle multiple HTTP actions, so we need to have only one controller method and check the actions and bodies when receiving the requests.

Long story short, we do not support RESTful APIs more easily because our route definitions do not have the HTTP actions.