How to create global constant variables in C# for the entire application

I have a table that contains system configuration (ie. system_name, system_time_zone.....)

I am trying to identify the best approach to read the configuration automaticly and have the value ready to read in any form in my application.

Basically, I want to read the configuration from the table and then set them as a constant variables. When the application loads I can re-use them in any form.

How would I be able to do this in C#?

Jon Skeet
people
quotationmark

Well, you could create a singleton class which would read the configuration on startup.

Personally, however, I would investigate dependency injection. There are lots of DI frameworks in .NET, and that way you'll have code which is easier to test, because it's given the configuration rather than it fetching it from somewhere which may be ugly to fake.

You may also want to consider separating the configuration out into different related parts, and only inject the parts of configuration that are relevant to a class into that class. For example, you might want one class holding all of the database-related settings, another for all the email-related settings (if it needs to send or retrieve emails) etc. That way it makes it obvious which aspects of configuration each part of your system depends on, which improves maintainability.

people

See more on this question at Stackoverflow