mercredi 3 mai 2023

Thin or Fat Structures

So say we have a User struct that represents some User object that in a web application. The User needs to be: Saved in the database Have their password set Sent an email to the attached email address

In general, there are two flavors of implenentation. One is to create a fat User class and add StoreUserinDB, SetPassword, SendEmail as methods to the User Class. This is the way that I am most naturally inclined to.

Another way is to encapsulate the three different methods into their own Classes/Modules, and then have a very thin User Class (basically zero methods, just fields) that interacts with each of these modules separately.

I know under SOLID design principles, the second way is preferred, but I would like to hear some personal anecdotes/gotchas for people who have used both methods, and could go through why one is preferrable over the other, and under which (all?) circumstances. On a personal note, I favor the first approach, because it keeps all of the logic and data in the same place, instead of forcing you to look across different locations to find the info.

Aucun commentaire:

Enregistrer un commentaire