Jaylen
Jaylen

Reputation: 40381

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#?

Upvotes: 0

Views: 2268

Answers (3)

Frank
Frank

Reputation: 4481

You could implement the Singleton Pattern, like this:

public class AppConfiguration
{
    private static readonly Lazy<AppConfiguration> Instance
        = new Lazy<AppConfiguration>(LoadConfiguration, LazyThreadSafetyMode.PublicationOnly);

    public static AppConfiguration Current
    {
        get { return Instance.Value; }
    }

    public string SystemName { get; private set; }

    public int SystemTimeZone { get; private set; }

    private AppConfiguration() {}

    private static AppConfiguration LoadConfiguration()
    {
        var configuration = new AppConfiguration();
        configuration.LoadFromDatabase();
        return configuration;
    }

    private void LoadFromDatabase()
    {
        using (var db = new AppEntities())
        {
            // Load configuration from DB

            this.SystemName = ""; // Load properties
        }
    }
}

And you can use it like this:

var systemName = AppConfiguration.Current.SystemName;

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1504132

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.

Upvotes: 6

John
John

Reputation: 3702

You could create a Singleton class for that:

public class ConfigurationSettings
{
    private ConfigurationSettings _instance;

    public int Setting1 { get; set; }

    public static ConfigurationSettings GetInstance()
    {
        return _instance;
    }

    // call this method once to load up all the data (at startup)
    public static void InitializeSingleInstance()
    {
        _instance = new ConfigurationSettings();
    }

    private ConfigurationSettings() 
    {
        // load from db / files / ... 
    }
}

Getting a setting would then go like:

int setting = ConfigurationSettings.GetInstance().Setting1;

Upvotes: 1

Related Questions