Is an object capable of storing itself in a DataBase messing up the Cohesion class?

Speaking of object-oriented design, do you think to give the opportunity to save yourself in the database so that the object spoils the COHESION class?

Imagine:

Product p = new Product() 
          {
           Name = "Joy Rider", 
           Price = 100, 
           Currency = "USD"
          };

Do you think that storing this product p in DataBase is better, you need to do this as follows:

 p.Save();

or in some way:

 ProductServices.SaveProduct(p);

What do you think?

+3
source share
3 answers

He intervenes in the principle of shared responsibility. The purpose of the Product class in your example is to represent the Product and operations on this product. Interacting with the database is not an essential part of class responsibility.

ProductServices . , ( ), ?

+5

, , SRP ( ).

- .

, LOW- - , , , , .

+9

- , Save Product. - . OO , , , .

, , , ; Save , . , Save, .

0

Source: https://habr.com/ru/post/1778978/


All Articles