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

后端 未结 7 815
深忆病人
深忆病人 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:32

    It's bad because (among other things) it violates the Principle of Least Astonishment.

    Programmers generally expect properties to do simple gets/sets. Encapsulating data access in a property, which could throw exceptions, cause side effects, and change the state of the data in the database, is not what is generally expected.

    I'm not saying there is no case for complex properties - sometimes, it can be a good solution. But, it is not the expected way to do things.

提交回复
热议问题