Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this questionI have various constants that my program uses... string
's, int
's,double
's, etc... What is the best way to store them? I don't think I want an Enum
, because the data is not all the same type, and I want to manually set each value. Should I just store them all in an empty class? Or is there a better way?
You probably could have them in a static class, with static read-only properties.
public static class Routes
{
public static string SignUp => "signup";
}
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.
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
orprotected
) 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
fieldsstatic
property withoutset
This is the best way IMO. No need for properties, or readonly:
public static class Constants
{
public const string SomeConstant = "Some value";
}
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.
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.
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
I would suggest static class with static readonly. Please find the code snippet below:
public static class CachedKeysManager
{
public static readonly string DistributorList = "distributorList";
}
Yes, a static class
for storing constants would be just fine, except for constants that are related to specific types.
精彩评论