database – Do I need to create separate classes for db entities and rest queries?

Question:

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 the object that the database stores must be sent by the server. Do I need to create a separate class for the database entity, a class that will be sent by the server, and a class that will execute business logic, or can you just create 1 class and decide before sending which fields to add to JSON and which to ignore?

Answer:

It all depends on the severity of the case.

Imagine, you have one table with a user, and 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 the unit tests. And it may not be you now, but you in 3 years, or your colleague.

On the other hand, if the application is one-off, then the game is not worth the trouble.

Scroll to Top