burnt1ce
burnt1ce

Reputation: 14917

How to store and retrieve constants that are not configurable in ASP.NET?

What is the best way for classes to retrieve/store constants without getting them through web.config?

Upvotes: 1

Views: 1297

Answers (4)

Cleiton
Cleiton

Reputation: 18153

XML files and use Application Cache to improve performance or uou can use ASP.NET Configuration Files.

Upvotes: 1

Pete
Pete

Reputation: 12593

That depends on how "constant" they are. If they are really constant, that is something that would never ever change, then a static class with constants in it. E.g. the number PI, or perhaps a constant containg the name of a field in a table.

If they are more server specific, like connection strings, and you don't want to use the web.config, you might want to check out the option of including a new config file from web.config

<appSettings file="local.config">...</appSettings>

The local.config should then just contain an appSettings element. If the local.config file doesn't exist, it will be ignored. So no exceptions. Any setting that exists in both local.config and web.config, the local.config will be used. You can then share a web config file between many installations, and have local settings overridden in the local.config.

If the constants are more something that a superuser should be able to modify at runtime, then a table in a database is what I would choose. E.g. a if you would let a superuser modify a max password retry attempt count or something.

Upvotes: 1

Mathieu Garstecki
Mathieu Garstecki

Reputation: 1629

Store them as static in a class, something like this:

public static class DbConstants
{
    public static const string CustomersTableName = "CUST";
    public static const string ProductsTableName = "PROD";
    ...
}

Consider separating them in several classes to group them logically, e.g. one class for DB constants, one class for exception messages, etc.

Upvotes: 5

Kelsey
Kelsey

Reputation: 47776

You include a static class with all your constants in it.

public static class MyConstants
{
    public const string MyString = "ConstString";
    public const int TestInt = 100;
    // etc...
}

Upvotes: 4

Related Questions