Which one best meets your need? Use this one. And only you can answer this.
Today there is a tendency to use API and use a frontend web that consumes that API equal to one frontend desktop or mobile, or some service would do. This has its usefulness there, and in some cases it is the best to do, especially when it is a web application and not a website. Although I am generally a critic of the web application, in most cases it should not be an application or it should not be web. I speak of the frontend.
If it is a website usually the traditional approach of working with the rendering of the page format be done on the server by view of the MVC or otherwise.
In this case there is no reason to have an extra layer. Only the view should be different, one generates JSON and the other generates HTML. If you need a controller different, or is doing something wrong or is using a rigid technology that does not meet the need.
Repeating the code in two places shouldn’t be an option. Even if the technology and/or architecture does not allow, it can still abstract the execution in functions in a separate service.
Ideally the controller should serve both well, if it is complicated, at least make him meet his real vocation which is to deliver the data to the view.
I see a lot of people writing that the logic of the application should be all there. I must have written it myself at some point. But then again, this is wrong, the controller must be the communication logic of the model with the vision. Execution that should not be in the model, must be in a specific part that is connected to the controller, but not directly. Of course, if it is simple, it has no use in more than one place, it is not wrong to embed right there.
Keep in mind that the actions of the controller is defined by how it receives the data and how it sends it, not the processing of the data. Not directly. There is no repetition, let alone hurt the DRY. Use a service.
If the API is lower level and the frontend is higher level, just use the little blocks of Lego loose in the first, and join them in the second. That’s why the processing should be separated when needed.
The lost art of abstracting
Today people are so worried about following cake recipes patterns, make crazy that she doesn’t understand architecture, that forget the basics of the basic, the foundation.
There are 3 things that revolutionized programming:
- Code writing at a high level
- Modularization
- Automatic management of memory
The second in its essence is to know how to use functions for your benefit, to avoid repetitions and achieve the DRY.
Today most codes do not meet this, many experienced people do not even realize it. Code organization is more important than following the master. Understanding the right level of abstraction helps you more than decorating ready-made models that may not be the most suitable for your problem.
For example, initially there was ASP.NET MVC and ASP.NET Webapi. Made by the best engineers in the world. It took them a long time to realize that everything was repeated and then they made ASP.NET Core to solve this problem.
If the controller is already on the same server as the database, I see no reason to make an extra request to fetch the data in the API.
– Woss
I imagined the same, but then I have to choose between making an extra request (which is made straight from the server, so I don’t know if it makes so much difference) or repeating the code in two different places, which makes maintenance difficult
– spacedogcs
And if the API already provides all the data, why do you need a controller? You can’t make the application serve directly in the API?
– Woss
Yes, but the API provides separate information, for example, people, products, brands, if I’m going to make a page that loads the 3, I need to either load by ajax, or make another function that calls the 3 requests
– spacedogcs
API with integrated website is still very confusing for me
– spacedogcs
And why, then, not define a Service that provides the data for both the controller and the API?
– Woss
Well, interestingly, I don’t know much about service, I’m going to do a little research on that, so thank you @Andersoncarloswoss
– spacedogcs