Answer the question
In order to leave comments, you need to log in
Where to place methods?
There are two classes Application (application, working with an object) and DB (for working with a database, getting / making records).
And there are two methods saveObject (saves an object of the Aplication class in the database) and getObject (Gets a record from the database and stores the Application object on it). The question is in which of these two classes is it better to place them and why? For these methods (as it seems to me) are connected with the functionality of both classes equally.
Answer the question
In order to leave comments, you need to log in
Follow the principle of least surprise. In other words, find 3-4 programmers whose opinion you value and respect, and ask "Where would you look in a class design that describes an application but whose state is read from the database?". If 2 programmers have a similar opinion, you will probably look in the same place in the future!
In general, both methods, in my opinion, refer to the level of data presentation, i.e. in the MVC pattern, this is the "Model" part. It seems to me that you should do this:
* These two methods should be placed in the same class. But not in Possible
* Think of an interface with these two methods, from it to generate a descendant, available DB. This is so as not to be tied to a specific location, suddenly next time it will not be DB, but Cloud or something else.
As far as I'm concerned, you answered your own question.
And in the Application class, you can get an object from the database through your DB class in order for the Application class to work with it.
Application (application, work with object )
A good solution would be to create a separate Loader class that will use the DB class (its generic methods) to retrieve the data. Something like the factory pattern.
It is better not to write such methods in the DB class itself, because very soon this class will have 100500 methods, two for each application class.
Also, you should not place such methods in the class whose data is stored in the database. The object itself must be able to work with the received data, and it doesn’t matter what source the data is received from - maybe you want to store this data not in the database, but in a file, or in a memcache.
If you follow the MVC methodology, then in asp.net mvc a model is a class that describes the table structure (properties) + methods of objects of this class.
If you follow the Perl + Mojolicious method, then just queries to sql go into my model, because I don’t use things like Hibernate
In any case, if you do not know how best, use the Tell, don't ask rule.
But the question is classic, it is a theory, and it has long been described in books.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question