C# read-only calculated properties, should they be methods?

前端 未结 11 1188
刺人心
刺人心 2021-01-31 13:55

I have several entities that have calculated fields on them such as TotalCost. Right now I have them all as properties but I\'m wondering if they should actually be methods. Is

11条回答
  •  执笔经年
    2021-01-31 14:41

    Sometimes, you have to consider also what you're modeling... On some domains, the calculated values are often or expected to be an attribute of the model -- a Property. If this were the case, then write it as a Property even though the calculation is not at all trivial or a little bit expensive to compute. Just document it on your API or implement some caching mechanism to minimize recomputation for this property.

提交回复
热议问题