存储我的程序使用的一组常量的最佳方法是什么?

发布于 2024-08-11 19:05:51 字数 1431 浏览 4 评论 0原文

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

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

发布评论

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

评论(9

夏天碎花小短裙 2024-08-18 19:05:51

您可能可以将它们放在静态类中,并具有静态只读属性。

public static class Routes
{
    public static string SignUp => "signup";
}

You probably could have them in a static class, with static read-only properties.

public static class Routes
{
    public static string SignUp => "signup";
}
北陌 2024-08-18 19:05:51

IMO 使用一个充满常量的类对于常量来说是很好的。如果它们偶尔会更改,我建议在您的配置中使用 AppSettings 和 ConfigurationManager 类。

当我有实际上从 AppSettings 或类似内容中提取的“常量”时,我仍然会始终有一个“常量”类来包装从配置管理器读取的内容。拥有 Constants.SomeModule.Setting 总是更有意义,而不必在任何想要使用所述内容的地方直接求助于 ConfigurationManager.AppSettings["SomeModule/Setting"]设定值。

此设置的优点是,由于 SomeModule 可能是 Constants 文件中的嵌套类,因此您可以轻松地使用依赖项注入将 SomeModule 直接注入到依赖于它的类中。您甚至可以在 SomeModule 之上提取一个接口,然后在您的使用代码中创建对 ISomeModuleConfiguration 的依赖关系,这样您就可以解耦对 Constants 文件的依赖关系,甚至可能使测试变得更容易,特别是如果这些设置来自 AppSettings 并且您使用配置转换来更改它们,因为这些设置是特定于环境的。

IMO using a class full of constants is fine for constants. If they will change semi-occasionally I recommend using AppSettings in your config and the ConfigurationManager class instead.

When I have "constants" that are actually pulled in from AppSettings or similar I still will always have a "constants" class that wraps the reading from configuration manager. It's always more meaningful to have Constants.SomeModule.Setting instead of having to resort directly to ConfigurationManager.AppSettings["SomeModule/Setting"] on any place that wants to consume said setting value.

Bonus points for this setup, since SomeModule would likely be a nested class inside the Constants file, you could easily use Dependency Injection to inject either SomeModule directly into classes that depend on it. You could also even extract an interface on top of SomeModule and then create a depenedency to ISomeModuleConfiguration in your consuming code, this would then allow you to decouple the dependency to the Constants files, and even potentially make testing easier, especially if these settings come from AppSettings and you change them using config transformations because the settings are environment specific.

屋顶上的小猫咪 2024-08-18 19:05:51

我喜欢做的是以下内容(但请确保读到最后以使用正确的常量类型):

internal static class ColumnKeys
{
    internal const string Date = "Date";
    internal const string Value = "Value";
    ...
}

阅读本文了解为什么 const 可能不是您想要的。可能的常量类型有:

  • const 字段。如果值将来可能发生变化,请勿跨程序集(publicprotected)使用,因为该值将在编译时在其他程序集中进行硬编码组件。如果更改该值,则其他程序集将使用旧值,直到重新编译它们为止。
  • static readonly 字段
  • static 属性,不带 set

What I like to do is the following (but make sure to read to the end to use the proper type of constants):

internal static class ColumnKeys
{
    internal const string Date = "Date";
    internal const string Value = "Value";
    ...
}

Read this to know why const might not be what you want. Possible type of constants are:

  • const fields. Do not use across assemblies (public or protected) if value might change in future because the value will be hardcoded at compile-time in those other assemblies. If you change the value, the old value will be used by the other assemblies until they are re-compiled.
  • static readonly fields
  • static property without set
拒绝两难 2024-08-18 19:05:51

在我看来,这是最好的方法。不需要属性,或者只读:

public static class Constants
{
   public const string SomeConstant = "Some value";
}

This is the best way IMO. No need for properties, or readonly:

public static class Constants
{
   public const string SomeConstant = "Some value";
}
喜你已久 2024-08-18 19:05:51

空的静态类是合适的。考虑使用多个类,这样您最终会得到一组良好的相关常量,而不是一个巨大的 Globals.cs 文件。

此外,对于某些 int 常量,请考虑符号:

[Flags]
enum Foo
{
}

因为这允许 将值视为标志

An empty static class is appropriate. Consider using several classes, so that you end up with good groups of related constants, and not one giant Globals.cs file.

Additionally, for some int constants, consider the notation:

[Flags]
enum Foo
{
}

As this allows for treating the values like flags.

囚你心 2024-08-18 19:05:51

另一次投票支持使用 web.config 或 app.config。配置文件是存放连接字符串等常量的好地方。我不想通过查看源代码来查看或修改这些类型的内容。从 .config 文件中读取这些常量的静态类可能是一个很好的折衷方案,因为它可以让您的应用程序访问这些资源,就像它们是在代码中定义的一样,但仍然可以灵活地以易于查看/编辑的方式显示它们空间。

Another vote for using web.config or app.config. The config files are a good place for constants like connection strings, etc. I prefer not to have to look at the source to view or modify these types of things. A static class which reads these constants from a .config file might be a good compromise, as it will let your application access these resources as though they were defined in code, but still give you the flexibility of having them in an easily viewable/editable space.

吻安 2024-08-18 19:05:51

我建议使用静态只读的静态类。请找到下面的代码片段:

  public static class CachedKeysManager
    {
        public static readonly string DistributorList = "distributorList";
    }

I would suggest static class with static readonly. Please find the code snippet below:

  public static class CachedKeysManager
    {
        public static readonly string DistributorList = "distributorList";
    }
別甾虛僞 2024-08-18 19:05:51

如果这些常量是影响应用程序行为的服务引用或开关,我会将它们设置为应用程序用户设置。这样,如果需要更改它们,您不必重新编译,并且仍然可以通过静态属性类引用它们。

Properties.Settings.Default.ServiceRef

If these Constants are service references or switches that effect the application behavior I would set them up as Application user settings. That way if they need to be changed you do not have to recompile and you can still reference them through the static properties class.

Properties.Settings.Default.ServiceRef
错爱 2024-08-18 19:05:51

是的,用于存储常量的静态类就可以了,但与特定类型相关的常量除外。

Yes, a static class for storing constants would be just fine, except for constants that are related to specific types.

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