Global constants in a separate file. Is that a good idea?

I am currently working on an ASP.NET MVC application. I plan to create a static class where I plan to hold all global string constants, such as session names.

I do not dare, because it is some kind of smell, but I do not know the best alternative.

Please show me how to define global constants.

+3
source share
4 answers

Vadim

I do what you propose and use a static class for this. Then you get the advantage of the strongly typed accessors PLUS the ability to add overrides (in the form of methods) if 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 use:

items = _repository.GetPaged(pageNumber, Config.PageSize)

"2- " web.config, , .. - , .

, , ( ) , .

+4

(.resx). , , web.config .

+1

global.asax - , .

private static int var ;

public static int VAR
{
   get { return var ; }
}
0

Whether it is MVC or web forms, I use a combination of database entries (for site settings that can be changed using the control panel) and web.config appSettings (for site settings that do not change often or in general, i.e. constant).

0
source

Source: https://habr.com/ru/post/1780080/


All Articles