Reputation: 21704
I'm currently working on ASP.NET MVC application. I'm planning to create a static class where I plan to hold all the global string constants like session names.
The reason I'm hesitant is because it's kind of smell but I'm not aware of better alternative.
Please show me the light how to define global constants.
Upvotes: 3
Views: 5784
Reputation: 136
You can use global.asax file for this purpose - I would use accessors for them e.g.
private static int var ;
public static int VAR
{
get { return var ; }
}
Upvotes: 0
Reputation: 22485
vadim,
i do exactly as you propose and use a static class for this purpose. You then get the advantage of strongly typed accessors PLUS the ability to add overrides (in the form of methods), should you require them.
here's a snippet:
public static class Config
{
private const string NotSet = "**VALUE NOT SET**";
private const int pageSize = 5;
public static string CustomCache
{
get
{
return ConfigurationManager.AppSettings["CustomCache"] ?? NotSet;
}
}
public static int PageSize
{
get
{
// simple default - no setter
return pageSize;
}
}
}
typical usage:
items = _repository.GetPaged(pageNumber, Config.PageSize)
in the above class, some settings are called '2nd generation' from the app settings in the web.config but with strong typing in the classes to ensure runtime error checking etc.. others are purely static settings defined in the class.
it's the flexibility to do all of the above that (in my opinion) gives this approach both appeal and a real strength.
Upvotes: 4
Reputation: 5381
Whether it is MVC or Web forms, I use a combination of database entries (for site settings that can be modified by dashboard) and web.config appSettings (for site settings that do not change often or at all, i.e. constant).
Upvotes: 0
Reputation: 74250
Another alternative would be to create a resources (.resx) file. Or if these are configurable values, they can go in web.config or a database configuration table.
Upvotes: 1