简体   繁体   中英

In which class should a GetAll method be?

Say I have a method GetAllEmails() which retrieves all emails from the database. The question is which class should be I putting the method to?

Many sources, even the textbooks, suggest it should be in the class Email , however logically an email should not know how to get all emails, and I think it should be placed in an email manager or helper class in such context. Any opinions?

存储库设计模式通常用于此,因此在您的情况下,它是电子邮件收发器(或类似的)类获取电子邮件。

Method GetAllEmails() has to connect to a DB and fetch some data. So I would put it in a class which is related into database. It really depends what sort of application you are writing, like if if you have some other methods which connects to database and fetch some other data, you can have them all in one class. But if you only connect to database for getting emails, then you can have a database subclass in your email class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM