Why should a C# property getter not read from a DB?

后端 未结 7 805
深忆病人
深忆病人 2020-12-20 16:12

Some of the answers and comments to this question: Simplest C# code to poll a property?, imply that retrieving data from a database in a property\'s getter is Generally a Ba

7条回答
  •  佛祖请我去吃肉
    2020-12-20 16:18

    Besides exceptions being likely, querying a database is a slooooow operation. In both aspects, using property getters as a database accessor violates the principle of least astonishment to client code. If I see a library class with a property, I don't expect it to do a lot of work, but to access a value that's easily retrieved.

    The least astonishing option here is to provide an old fashioned, simple Get function.

提交回复
热议问题