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

后端 未结 7 800
深忆病人
深忆病人 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条回答
  •  旧时难觅i
    2020-12-20 16:26

    A getter, by definition, should just be encapsulating data, not functionality.

    Also, you would likely be redefining functionality and making many trips to the database if you had more than one getter that needed to round-trip to the database. Why not handle that in a centralized place rather than splitting that out among multiple properties?

    Methods are for class-level data modification and functionality, properties are for individual data modification and retrieval only.

提交回复
热议问题