I have a controller that accepts or returns a user object. I also have a database that stores the user. Naturally, not all fields of an object that stores a database should be sent by the server. Do I need to create a separate class for the entity bd, a class that will be sent by the server, and a class that will execute business logic, or you can simply create 1 class and decide which fields to add to JSON and which ones to ignore before sending it?

    2 answers 2

    It all depends on the severity of the case.

    Imagine, you have one table with the user, and there are two forms where user information is displayed. And then you want to add a third without hitting the first two. In the case of separate classes for each form (request), you will add a new class and a new form, and in the case of one class you will have to go into the already working (tested) code, change unit tests. And it may not be you now, but you after 3 years, or your colleague.

    On the other hand, if the application is disposable, then the game is not worth the candle.

      If you plan to follow the MVC pattern and at the same time want to exclude some data from the entity before sending it to the client, then it is better to create a separate class for this and send it instead of the original model.

      Creating and populating an instance of this class with data can be performed in the controller or, as an option in the original model, using the appropriate method.