为什么不从数据库读取C#属性获取器? [英] Why should a C# property getter not read from a DB?

查看:41
本文介绍了为什么不从数据库读取C#属性获取器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的一些答案和评论:要轮询的最简单C#代码一个属性?,这意味着从属性的getter中从数据库中检索数据通常是个坏主意.
为什么这么糟糕?

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 Bad Idea.
Why is it so bad?

(如果您有信息的来源,请提及它们.)

(If you have sources for your information, please mention them.)

如果会影响您的回答,通常我会在第一个获取"之后将信息存储在变量中以供重用.

I will usually be storing the information in a variable after the first "get" for reuse, if that influences your answer.

推荐答案

因为从数据库中检索数据可能会导致任意数量的异常,并且通常,属性获取器永远都不应抛出异常.

Because retrieving data from a database could cause any number of exceptions, and property getters, as a rule, should never throw exceptions.

属性获取器的预期行为只是返回一个值.如果实际上它做的还不止于此,那应该是一种方法.

The expected behavior of a property getter is just to return a value; if it's actually doing a lot more than that, it should be a method.

Microsoft的《物业设计指南》解释了以下原因: https://docs.microsoft.com/zh-cn/dotnet/standard/design-guidelines/property

Microsoft's guide for Property Design explains the reasons: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/property

这篇关于为什么不从数据库读取C#属性获取器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆