从 Web.Config 检索时如何将变量定义为 CONSTANT?

发布于 2024-07-17 22:29:35 字数 802 浏览 4 评论 0原文

我在 AppSettings 中保留了很多设置,我想知道以大写命名它们是否被认为是一个好习惯。 本质上,它们与常量相同,对吧? 据我了解,如果您更改 Web.Config,应用程序会重新编译。

所以,我在想,您是否应该将 AppSettings 中的设置保留为大写(假设您使用全部大写来命名常量。)

此外,从 AppSettings 获取值的变量是否应该大写?

例如。

String MY_SETTING = ConfigurationManager.AppSettings["MY_SETTING"];

处理这些并使它们看起来和感觉起来像常量的最佳方法是什么? 这是个好主意吗? 我能想到的唯一方法就是使其只读:

readonly String MY_SETTING = ConfigurationManager.AppSettings["MY_SETTING"];

但是我不知道如何用 int 来做到这一点:

readonly String MAX_USERS_S = ConfigurationManager.AppSettings["MAX_USERS"];
readonly int MAX_USERS; // needs to be set here... won't compile
int.TryParse(MAX_USERS_S, out MAX_USERS);

我不知何故觉得将只读变量设置为看起来像常量很肮脏,但对我来说,这是网络中的东西。配置基本上是不变的。

建议?

I keep a lot of settings in AppSettings, and I was wondering if it's considered good practice to name them in UpperCase. Essentially, they're the same as Constants right? As I understand it, if you change the Web.Config, the app does a recompile.

So, I was thinking, should you keep the settings in AppSettings in UPPERCASE (Assuming you name your constants with all UPPER case.)

Also, should variables that get values from AppSettings be UPPERCASE?

EG.

String MY_SETTING = ConfigurationManager.AppSettings["MY_SETTING"];

What is the best way to handle these and make them look and feel like Constants? Is it even a good idea? The only way I could think of would be make it readonly:

readonly String MY_SETTING = ConfigurationManager.AppSettings["MY_SETTING"];

But then I don't know how you could do this with an int:

readonly String MAX_USERS_S = ConfigurationManager.AppSettings["MAX_USERS"];
readonly int MAX_USERS; // needs to be set here... won't compile
int.TryParse(MAX_USERS_S, out MAX_USERS);

I somehow feel dirty setting readonly variables to look like constants, but to me, stuff in the web.config are essentially constant.

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

⊕婉儿 2024-07-24 22:29:35

如果您认为它们实际上是常量,并且它们是不可变类型(intstring 等)的只读变量,那么请务必使用相同的命名约定。 事实上,这就是 String.Empty 确实如此。 但是,常量的.NET 命名约定不是SHOUTY_CAPS,它是 PascalCase

If you feel they're effectively constant, and they're readonly variables of an immutable type (int, string etc) then by all means use the same naming convention. Indeed, this is what String.Empty does. However, the .NET naming convention for constants isn't SHOUTY_CAPS, it's PascalCase.

猫瑾少女 2024-07-24 22:29:35

如果您希望它们看起来和感觉起来像常量,我建议的第一个地方是使用 Pascal 大小写命名它们,因此 MY_SETTING(实际上不是一个类似 C# 的名称)将是“MySetting”。 您会在框架中看到很多公开的属性和常量。

readonly 是这里的正确方法。 对于实例字段,只能在声明或构造函数中设置。 对于静态字段,只能在静态构造函数或声明中设置。

对于对 TryParse 的调用,您可以在构造函数或静态构造函数中设置它,具体取决于它是实例还是静态字段:

static MyClass()
{
    // The value.
    int myUsers;

    // Try to parse.
    if (int.TryParse(MAX_USERS_S, out myUsers))
    {
        // Assign.
        MyUsers = myUsers;
    }
}

If you want them to look and feel like constants, the first place I would recommend is naming them with Pascal-case, so MY_SETTING (which isn't really a C#-like name) would be "MySetting". You see this a great deal with publicly exposed properties and constants in the framework.

readonly is the correct way to go here. In the case of an instance field, it can only be set in the declaration or the constructor. For static fields, it may only be set in the static constructor or declaration.

For the call to TryParse, you would set it in your constructor, or the static constructor, depending on whether or not it was an instance or static field:

static MyClass()
{
    // The value.
    int myUsers;

    // Try to parse.
    if (int.TryParse(MAX_USERS_S, out myUsers))
    {
        // Assign.
        MyUsers = myUsers;
    }
}
安静 2024-07-24 22:29:35

No. There is no cases of all-uppercase naming in the .NET naming guidelines.

心奴独伤 2024-07-24 22:29:35

据我了解,如果您更改 Web.Config,应用程序会重新编译。

没错,对 Web.Config 的任何更改都将重新编译应用程序。 甚至像换行符一样小的变化。


忽略命名约定问题,您可以对示例执行以下操作:

readonly int MAX_USERS = 
    int.Parse(ConfigurationManager.AppSettings["MAX_USERS"])

显然,如果 MAX_USERS 不是 int,则这不是一种安全的方法,但似乎在以下情况下抛出异常:无法设置 MAX_USERS 是正确的做法。

As I understand it, if you change the Web.Config, the app does a recompile.

That's correct, any change to the Web.Config will recompile the app. Even changes as small as a newline.


Ignoring the naming conventions question, you could do the following with your example:

readonly int MAX_USERS = 
    int.Parse(ConfigurationManager.AppSettings["MAX_USERS"])

Obviously this is not a safe way to do this in the event that MAX_USERS is not an int, but it seems that throwing an exception when MAX_USERS can't be set would be the correct course of action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文