DEV Community 👩‍💻👨‍💻

Discussion on: Entities properties : public, protected or private ?

Collapse
 
sergey_telpuk profile image
Sergey Telpuk • Edited on

An entity is a mutable object, so entity properties should be public, but if you want to guarantee that your entity must always have assigned particular property, make them private and require to pass via the constructor. See example,

Class Test
 private a

 Constructor(а){
  This->а = а;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
juyn profile image
Xavier Dubois 🇫🇷 Author

Well, I do see the logic here, but if your concern is to guarantee that properties are set, using assertions and validators is easier and safer.
Constructors are good for tiny entities, but if they have more than 10 properties, good luck with that !

Collapse
 
sergey_telpuk profile image
Sergey Telpuk • Edited on

No problem, do you know about value object? You always wrap up and pass it.
Please spend time on reading this book. After that, your doubts dissipate.